類的反射器:
python class.py sys.argv[1]
#/usr/bin/env python # -*- coding:utf-8 -*- import sys class webserver(object): def __init__(self,host,port): self.host = host self.port = port def start(self): print('server %s is starting'%self.host) def stop(self): print('server %s is stoping'%self.host) def restart(self): self.start() self.stop() if __name__ == "__main__": server = webserver('localhost','333') if hasattr(server,sys.argv[1]): #判斷參數是否在實例中 func = getattr(server,sys.argv[1])#獲取實例的內存地址 func()#執行實例的方法可以傳參數
2.hashlib
>>> import hashlib
>>> m= hashlib.md5()
>>> m.update("www")
>>> m.update(b"123")
>>> print(m.hexdigest())
202cb962ac59075b964b07152d234b70
3. 異常處理
#/usr/bin/env python # -*- coding:utf-8 -*- while True: num1 = input("num1:") num2 = input("num2:") try: num1 = int(num1) num2 = int(num2) sum = num1 + num2 except ValueError as e: print("vcalue err",e) except IndentationError as e: print("inde err",e) except Exception as e:#3.5寫法 Exception 抓取所有錯誤 print("信息錯誤,請重新輸入!!") print(e) print(sum)
4.線程和進程
線程是操作系統能夠進行運算調度的最小單位,它包含在進程之中,是進程中的實際運作單位。一條線程指的是進程中一個單一順序的控制流,一個進程中可以并發多個線程,每條線程并行執行不同的任務
進程:進程是一個具有獨立功能的程序關于某個數據集合的一次運行活動。它可以申請和擁有系統資源,是一個動態的概念,是一個活動的實體。它不只是程序的代碼,還包括當前的活動,通過程序計數器的值和處理寄存器的內容來表示。
4.多線程實例:
#/usr/bin/env python # -*- coding:utf-8 -*- import threading import time def sayhi(num):#定義每個線程要運行的函數 print("running on number:%s"%num) time.sleep(3) if __name__ == '__main__': t1 = threading.Thread(target=sayhi,args=(1,)) #生成一個線程實例 t2 = threading.Thread(target=sayhi,args=(2,)) #生成一個線程實例 t1.start() t2.start() print(t1.getName()) #獲取線程名 print(t2.getName())
#/usr/bin/env python # -*- coding:utf-8 -*- import threading import time class MyThread(threading.Thread): def __init__(self,num): threading.Thread.__init__(self) self.num = num def run(self):#定義每個線程要運行的函數 run寫死的不能叫其他名字 print("running on number:%s"%self.num) time.sleep(3) if __name__ == '__main__': t1 = MyThread(1) t2 = MyThread(2) t1.start() t2.start()
如果后續需要繼續執行等待前面線程結束使用t1.john()函數
#/usr/bin/env python # -*- coding:utf-8 -*- import threading import time def sayhi(num):#定義每個線程要運行的函數 print("running on number:%s"%num) time.sleep(3) if __name__ == '__main__': ''' t1 = threading.Thread(target=sayhi,args=(1,)) #生成一個線程實例 t2 = threading.Thread(target=sayhi,args=(2,)) #生成一個線程實例 t1.start() t2.start() print(t1.getName()) #獲取線程名 print(t2.getName()) ''' t_list = []#使用阻塞空列表 for i in range(10): t = threading.Thread(target=sayhi,args=[i,]) t.start() t_list.append(t) for i in t_list: i.join() print('#'*5)
#/usr/bin/env python # -*- coding:utf-8 -*- import threading import time def addNum(): global num #在每個線程中都獲取這個全局變量 print("--get num:",num) time.sleep(1) lock.acquire()#枷鎖不能對時間 num -=1 #對此公告變量減一 lock.release()#釋放鎖 lock = threading.Lock()#線程鎖 num = 100 thread_list = [] for i in range(100): t = threading.Thread(target=addNum) t.start() thread_list.append(t) for i in thread_list: i.join() print("final num:",num)
#/usr/bin/env python # -*- coding:utf-8 -*- import threading import time def addNum(n): semapore.acquire()#獲取信號量 time.sleep(1) print("num:%s"%n) semapore.release()#釋放信號量 semapore = threading.BoundedSemaphore(2)#最大兩個線程同時運行 thread_list = [] for i in range(20): t = threading.Thread(target=addNum,args=[i,]) t.start() thread_list.append(t) for i in thread_list: i.join()
浙公網安備 33010602011771號