Python 多線程之threading 模塊的使用
簡介
Python 通過 _thread 和 threading 模塊提供了對多線程的支持,threading 模塊兼具了 _thread 模塊的現(xiàn)有功能,又擴展了一些新的功能,具有十分豐富的線程操作功能
創(chuàng)建線程
使用 threading 模塊創(chuàng)建線程通常有兩種方式:
1)使用 threading 模塊中 Thread 類的構(gòu)造器創(chuàng)建線程,即直接對類 threading.Thread 進行實例化,并調(diào)用實例化對象的 start 方法創(chuàng)建線程;
2)繼承 threading 模塊中的 Thread 類創(chuàng)建線程類,即用 threading.Thread 派生出一個新的子類,將新建類實例化,并調(diào)用其 start 方法創(chuàng)建線程。
構(gòu)造器方式
調(diào)用 threading.Thread 類的如下構(gòu)造器創(chuàng)建線程:
threading.Thread(group=None, target=None, name=None, args=(), kwargs={}, *, daemon=None) group:指定該線程所屬的線程組,目前該參數(shù)還未實現(xiàn),為了日后擴展 ThreadGroup 類實現(xiàn)而保留。 target:用于 run() 方法調(diào)用的可調(diào)用對象,默認是 None,表示不需要調(diào)用任何方法。 args:是用于調(diào)用目標函數(shù)的參數(shù)元組,默認是 ()。 kwargs:是用于調(diào)用目標函數(shù)的關(guān)鍵字參數(shù)字典,默認是 {}。 daemon:如果 daemon 不是 None,線程將被顯式的設(shè)置為守護模式,不管該線程是否是守護模式,如果是 None (默認值),線程將繼承當前線程的守護模式屬性。
import time import threading def work(num): print('線程名稱:',threading.current_thread().getName(),'參數(shù):',num,'開始時間:',time.strftime('%Y-%m-%d %H:%M:%S')) if __name__ == '__main__': print('主線程開始時間:',time.strftime('%Y-%m-%d %H:%M:%S')) t1 = threading.Thread(target=work,args=(3,)) t2 = threading.Thread(target=work,args=(2,)) t3 = threading.Thread(target=work,args=(1,)) t1.start() t2.start() t3.start() t1.join() t2.join() t3.join() print('主線程結(jié)束時間:', time.strftime('%Y-%m-%d %H:%M:%S'))
上述示例中實例化了三個 Thread 類的實例,并向任務(wù)函數(shù)傳遞不同的參數(shù),start
方法開啟線程,join
方法阻塞主線程,等待當前線程運行結(jié)束。
繼承方式
通過繼承的方式創(chuàng)建線程包括如下步驟:1)定義 Thread 類的子類,并重寫該類的 run 方法;2)創(chuàng)建 Thread 子類的實例,即創(chuàng)建線程對象;3)調(diào)用線程對象的 start 方法來啟動線程。示例如下:
import time import threading class MyThread(threading.Thread): def __init__(self,num): super().__init__() self.num = num def run(self): print('線程名稱:', threading.current_thread().getName(), '參數(shù):', self.num, '開始時間:', time.strftime('%Y-%m-%d %H:%M:%S')) if __name__ == '__main__': print('主線程開始時間:',time.strftime('%Y-%m-%d %H:%M:%S')) t1 = MyThread(3) t2 = MyThread(2) t3 = MyThread(1) t1.start() t2.start() t3.start() t1.join() t2.join() t3.join() print('主線程結(jié)束時間:', time.strftime('%Y-%m-%d %H:%M:%S'))
上述示例中自定義了線程類 MyThread,繼承了 threading.Thread,并重寫了 __init__ 方法和 run 方法。
守護線程
守護線程(也稱后臺線程)是在后臺運行的,它的任務(wù)是為其他線程提供服務(wù),如 Python 解釋器的垃圾回收線程就是守護線程。如果所有的前臺線程都死亡了,守護線程也會自動死亡。來看個例子:
# 不設(shè)置守護線程 import threading def work(num): for i in range(num): print(threading.current_thread().name + " " + str(i)) t = threading.Thread(target=work, args=(10,), name='守護線程') t.start() for i in range(10): pass
# 設(shè)置守護線程 import threading def work(num): for i in range(num): print(threading.current_thread().name + " " + str(i)) t = threading.Thread(target=work, args=(10,), name='守護線程') t.daemon = True t.start() for i in range(10): pass
上述示例直觀的說明了當前臺線程結(jié)束,守護線程也會自動結(jié)束。
如果你設(shè)置一個線程為守護線程,就表示這個線程是不重要的,在進程退出的時候,不用等待這個線程退出;如果你的主線程在退出的時候,不用等待哪些子線程完成,那就設(shè)置這些線程為守護線程;如果你想等待子線程完成后再退出,那就什么都不用做,或者顯示地將 daemon 屬性設(shè)置為 false。
線程本地數(shù)據(jù)
Python 的 threading 模塊提供了 local 方法,該方法返回得到一個全局對象,不同線程使用這個對象存儲的數(shù)據(jù),其它線程是不可見的(本質(zhì)上就是不同的線程使用這個對象時為其創(chuàng)建一個獨立的字典)。來看個示例:
# 不使用 threading.local import threading import time num = 0 def work(): global num for i in range(10): num += 1 print(threading.current_thread().getName(), num) time.sleep(0.0001) for i in range(5): threading.Thread(target=work).start()
上面示例中 num 是全局變量,變成了公共資源,通過輸出結(jié)果,我們發(fā)現(xiàn)子線程之間的計算結(jié)果出現(xiàn)了互相干擾的情況。
# 使用 threading.local num = threading.local() def work(): num.x = 0 for i in range(10): num.x += 1 print(threading.current_thread().getName(), num.x) time.sleep(0.0001) for i in range(5): threading.Thread(target=work).start()
使用 threading.local 的示例中,num 是全局變量,但每個線程定義的屬性 num.x 是各自線程獨有的,其它線程是不可見的,因此每個線程的計算結(jié)果未出現(xiàn)相互干擾的情況。
定時器
threading 模塊提供了 Timer 類實現(xiàn)定時器功能,來看個例子:
# 單次執(zhí)行 from threading import Timer def work(): print("Hello Python") # 5 秒后執(zhí)行 work 方法 t = Timer(5, work) t.start()
Timer 只能控制函數(shù)在指定的時間內(nèi)執(zhí)行一次,如果我們需要多次重復(fù)執(zhí)行,需要再進行一次調(diào)度,想要取消調(diào)度時可以使用 Timer 的 cancel 方法。來看個例子:
# 重復(fù)執(zhí)行 count = 0 def work(): print('當前時間:', time.strftime('%Y-%m-%d %H:%M:%S')) global t, count count += 1 # 如果 count 小于 5,開始下一次調(diào)度 if count < 5: t = Timer(1, work) t.start() # 指定 2 秒后執(zhí)行 work 方法 t = Timer(2, work) t.start()
以上就是Python 多線程之threading 模塊的使用的詳細內(nèi)容,更多關(guān)于python threading的使用的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
獲取python的list中含有重復(fù)值的index方法
今天小編就為大家分享一篇獲取python的list中含有重復(fù)值的index方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-06-06Selenium向iframe富文本框輸入內(nèi)容過程圖解
這篇文章主要介紹了Selenium向iframe富文本框輸入內(nèi)容過程圖解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-04-04python 讀寫、創(chuàng)建 文件的方法(必看)
下面小編就為大家?guī)硪黄猵ython 讀寫、創(chuàng)建 文件的方法(必看)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-09-09python中的[1:]、[::-1]、X[:,m:n]和X[1,:]的使用
本文主要介紹了python中的[1:]、[::-1]、X[:,m:n]和X[1,:]的使用,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08Python通過pymysql調(diào)用MySQL進行增刪改移查
這篇文章主要介紹了Python通過pymysql調(diào)用MySQL,從而實現(xiàn)數(shù)據(jù)的增刪改移查功能,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2021-12-12Python readline()和readlines()函數(shù)實現(xiàn)按行讀取文件
本文主要介紹了Python readline()和readlines()函數(shù)實現(xiàn)按行讀取文件,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02Python變量、數(shù)據(jù)類型、數(shù)據(jù)類型轉(zhuǎn)換相關(guān)函數(shù)用法實例詳解
這篇文章主要介紹了Python變量、數(shù)據(jù)類型、數(shù)據(jù)類型轉(zhuǎn)換相關(guān)函數(shù)用法,結(jié)合實例形式詳細分析了Python變量類型、基本用法、變量類型轉(zhuǎn)換相關(guān)函數(shù)與使用技巧,需要的朋友可以參考下2020-01-01