Python-Thread(通俗易懂)
此類表示在單獨的控制線程中運行的活動,有兩種方法可以指定該活動,一是將可調用對象傳遞給構造函數,二是通過覆蓋子類中的run()方法。
如果你對線程不太理解,我們可以打個比方,把線程數看作車輛數,我們來完成一個簡單的客運運輸工作(以下為了方便理解,也加入相應註釋)。
更多threading模塊函數和對象說明,可參考:https://www.cnblogs.com/leozhanggg/p/10317494.html
一、無線程:
示例:
import time
start = time.time() people = 500 # 假設有500個人 def action(num):global people while people>0: people -= 50 # 每次運輸50人 print("車輛編號:%d, 當前車站人數:%d" %(num, people)) time.sleep(1) num = 1 # 車輛編號 action(num) end = time.time() print("Duration time: %0.3f" %(end-start))
運行結果:
C:\Python37\python.exe Y:/Project-python/threading/test.py 車輛編號:1, 當前車站人數:450 車輛編號:1, 當前車站人數:400 車輛編號:1, 當前車站人數:350 車輛編號:1, 當前車站人數:300 車輛編號:1, 當前車站人數:250 車輛編號:1, 當前車站人數:200 車輛編號:1, 當前車站人數:150 車輛編號:1, 當前車站人數:100 車輛編號:1, 當前車站人數:50 車輛編號:1, 當前車站人數:0 Duration time: 10.001 Process finished with exit code 0
二、單線程:
編碼示例:
import threading import time
start = time.time() people = 500 #假設有500個人 def action(num): global people while people>0: people -= 50 # 每次運輸50人 print("車輛編號:%d, 當前車站人數:%d" %(num, people)) time.sleep(1) num = 1 # 車輛編號 vehicle = threading.Thread(target=action, args=(num,)) # 新建車輛 vehicle.start() # 啟動車輛 vehicle.join() # 檢查到站車輛 end = time.time() print("Duration time: %0.3f" %(end-start))
運行結果:
C:\Python37\python.exe Y:/Project-python/threading/test.py 車輛編號:1, 當前車站人數:450 車輛編號:1, 當前車站人數:400 車輛編號:1, 當前車站人數:350 車輛編號:1, 當前車站人數:300 車輛編號:1, 當前車站人數:250 車輛編號:1, 當前車站人數:200 車輛編號:1, 當前車站人數:150 車輛編號:1, 當前車站人數:100 車輛編號:1, 當前車站人數:50 車輛編號:1, 當前車站人數:0 Duration time: 10.001 Process finished with exit code 0
三、多線程(傳遞對象方式):
編碼示例:
# -*- coding: utf-8 -* import threading import time people = 500 class MyThread(threading.Thread): def __init__(self, num): super(MyThread, self).__init__() self.num = num def run(self): global people while people > 0: people -= 50 print("車輛編號:%d, 當前車站人數:%d" % (self.num, people)) time.sleep(1) start = time.time() vehicles = [] # 新建車輛組 for num in range(5): # 設置車輛數 vehicle = MyThread(num) # 新建車輛 vehicles.append(vehicle) # 添加車輛到車輛組中 vehicle.start() #啟動車輛 for vehicle in vehicles: vehicle.join() # 分別檢查到站車輛 end = time.time() print("Duration time: %0.3f" % (end-start))
運行結果:
C:\Python37\python.exe Y:/Project-python/threading/test.py 車輛編號:0, 當前車站人數:450 車輛編號:1, 當前車站人數:400 車輛編號:2, 當前車站人數:350 車輛編號:3, 當前車站人數:300 車輛編號:4, 當前車站人數:250 車輛編號:2, 當前車站人數:200 車輛編號:1, 當前車站人數:150 車輛編號:0, 當前車站人數:100 車輛編號:3, 當前車站人數:50 車輛編號:4, 當前車站人數:0 Duration time: 2.001 Process finished with exit code 0
四、多線程(覆蓋子類方式)
編碼示例:
# -*- coding: utf-8 -* import threading import time people = 500 class MyThread(threading.Thread): def __init__(self, num): super(MyThread, self).__init__() self.num = num def run(self): global people while people > 0: people -= 50 print("車輛編號:%d, 當前車站人數:%d" % (self.num, people)) time.sleep(1) start = time.time() vehicles = [] for num in range(5): vehicle = MyThread(num) vehicles.append(vehicle) vehicle.start() for vehicle in vehicles: vehicle.join() end = time.time() print("Duration time: %0.3f" % (end-start))
運行結果:
C:\Python37\python.exe Y:/Project-python/threading/test.py 車輛編號:0, 當前車站人數:450 車輛編號:1, 當前車站人數:400 車輛編號:2, 當前車站人數:350 車輛編號:3, 當前車站人數:300 車輛編號:4, 當前車站人數:250 車輛編號:0, 當前車站人數:200 車輛編號:2, 當前車站人數:150車輛編號:3, 當前車站人數:100車輛編號:1, 當前車站人數:50 車輛編號:4, 當前車站人數:0 Duration time: 2.003 Process finished with exit code 0
五、結果分析
1. 通過結果不難發現,不使用線程類和使用單線程運行時間是一樣的,因為我們正常執行一個腳本,本質上就是單線程。
2. 創建多線程的兩種方法運行時間也是一樣的,因為最終都是交給Thread類來處理,自行選擇即可。
3. 多線程運行時間明顯比單線程快的多,從理論上來說是和線程數成正比的,但是實際並非是線程越多越好,因為線程越多所消耗的資源也就越多。
六、有關該類的其他說明:
a. 創建線程對象後,必須通過調用線程的start()方法啟動其活動,這將在單獨的控制線程中調用run()方法。
b. 一旦線程的活動開始,線程就被認為是“活著的”,當run()方法終止時,它會停止活動,或者引發異常。
c. 線程可以調用is_alive()方法測試是否處於活動狀態,其他線程可以調用線程的join()方法,這將阻塞調用線程,直到調用其join()方法的線程終止。
d. 線程有一個名稱,這個名稱可以傳遞給構造函數,並通過name屬性讀取或更改。
e. 線程可以標記為“守護程序線程”,這個標誌的意義在於,當只剩下守護進程線程時,整個Python程序都會退出,可以通過守護程序屬性設置該標誌。
----- 菜鳥隨筆,歡迎各路大神指點。
----- 轉載請註明原作,謝謝:https://www.cnblogs.com/leozhanggg/p/10335098.html
Python-Thread(通俗易懂)