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. 執行緒可以呼叫i s_alive ()方法測試是否處於活動狀態,其他執行緒可以呼叫執行緒的 join ()方法,這將阻塞呼叫執行緒,直到呼叫其 join ()方法的執行緒終止。
d. 執行緒有一個名稱,這個名稱可以傳遞給建構函式,並通過 name 屬性讀取或更改。
e. 執行緒可以標記為“守護程式執行緒”,這個標誌的意義在於,當只剩下守護程序執行緒時,整個Python程式都會退出,可以通過守護程式屬性設定該標誌。
----- 菜鳥隨筆,歡迎各路大神指點。
----- 轉載請註明原作,謝謝: https://www.cnblogs.com/leozhanggg/p/10335098.html