Python?中?threading.Thread.join()?的使用方法示例詳解
threading.Thread.join() 方法用于阻塞當(dāng)前線程,直到調(diào)用它的線程對象執(zhí)行完成或者超時。這在需要等待子線程執(zhí)行完畢后再繼續(xù)執(zhí)行主線程時非常有用?;谒@種特性,我講用我的方法幫你選擇你合適的解決方案。

問題背景
在 Python 中,想要充分利用多線程的優(yōu)勢,就需要對 threading 模塊中的 Thread 類有一定的了解。這里有一個非常簡單的多線程程序,用于幫助我們理解 threading.Thread.join 方法。
import threading
val = 0
def increment():
global val
print("Inside increment")
for x in range(100):
val += 1
print("val is now {} ".format(val))
thread1 = threading.Thread(target=increment, args=())
thread2 = threading.Thread(target=increment, args=())
thread1.start()
# thread1.join()
thread2.start()
# thread2.join()這里有兩個問題:
如果注釋掉 thread1.join() 和 thread2.join(),那么輸出結(jié)果會是怎樣的?如果不注釋掉 thread1.join() 和 thread2.join(),那么輸出結(jié)果又會是怎樣的?
解決方法
1. 不注釋掉 join() 方法
如果我們不注釋掉 thread1.join() 和 thread2.join(),那么輸出結(jié)果如下:
Inside increment val is now 1 val is now 2 val is now 3 ... val is now 100 Inside increment val is now 1 val is now 2 val is now 3 ... val is now 100
2. 注釋掉 join() 方法
如果我們注釋掉 thread1.join() 和 thread2.join(),那么輸出結(jié)果如下:
Inside increment Inside increment val is now 1 val is now 1 val is now 2 val is now 3 ... val is now 99 val is now 2 val is now 3 ... val is now 98 val is now 99 val is now 100
比較輸出結(jié)果
通過比較這兩個輸出結(jié)果,我們可以發(fā)現(xiàn),如果注釋掉 join() 方法,那么兩個線程的輸出結(jié)果是交織在一起的,這表明這兩個線程是并發(fā)執(zhí)行的。而如果不注釋掉 join() 方法,那么兩個線程的輸出結(jié)果是按照順序輸出的,這表明這兩個線程是串行執(zhí)行的。
join() 方法的作用
join() 方法的作用是讓調(diào)用它的線程等待另一個線程終止。在我們的例子中,thread1.join() 和 thread2.join() 的作用是讓主線程等待 thread1 和 thread2 兩個線程終止。如果不注釋掉這兩個方法,那么主線程就會等待這兩個線程終止后才繼續(xù)執(zhí)行。而如果注釋掉這兩個方法,那么主線程就不會等待這兩個線程終止,而是直接繼續(xù)執(zhí)行。
代碼示例:
為了更清楚地了解 join() 方法的作用,我們修改一下上面的代碼:
import threading
val = 0
def increment(msg,sleep_time):
global val
print("Inside increment")
for x in range(10):
val += 1
print("%s : %d\n" % (msg,val))
time.sleep(sleep_time)
thread1 = threading.Thread(target=increment, args=("thread_01",0.5))
thread2 = threading.Thread(target=increment, args=("thread_02",1))
thread1.start()
thread1.join()
thread2.start()
thread2.join()如果我們運行這段代碼,那么輸出結(jié)果如下:
Inside increment
thread_01 : 1thread_01 : 2
thread_01 : 3
thread_01 : 4
thread_01 : 5
thread_01 : 6
thread_01 : 7
thread_01 : 8
thread_01 : 9
thread_01 : 10
Inside increment
thread_02 : 1thread_02 : 2
thread_02 : 3
thread_02 : 4
thread_02 : 5
thread_02 : 6
thread_02 : 7
thread_02 : 8
thread_02 : 9
thread_02 : 10
從輸出結(jié)果中,我們可以看到,這兩個線程是按照順序輸出的,這表明這兩個線程是串行執(zhí)行的。這是因為我們在代碼中使用了 thread1.join() 和 thread2.join() 這兩個方法,讓主線程等待這兩個線程終止后才繼續(xù)執(zhí)行。
在這個例子中,主線程啟動了一個子線程,并在子線程執(zhí)行完成之前調(diào)用了 join() 方法來等待子線程執(zhí)行完成。如有任何疑問可以評論區(qū)留言討論。
到此這篇關(guān)于Python 中 threading.Thread.join() 的使用方法的文章就介紹到這了,更多相關(guān)Python threading.Thread.join()內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決tf.keras.models.load_model加載模型報錯問題
這篇文章主要介紹了解決tf.keras.models.load_model加載模型報錯問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06
Python中Dataframe元素為不定長list時的拆分分組
本文主要介紹了Python中Dataframe元素為不定長list時的拆分分組,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03
pytorch?tensor按廣播賦值scatter_函數(shù)的用法
這篇文章主要介紹了pytorch?tensor按廣播賦值scatter_函數(shù)的用法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06
Python pip通過requirements.txt 文件安裝依賴
requirements.txt是定義項目依賴的python包,可通過工具生成,本文主要介紹了Python pip通過requirements.txt文件安裝依賴,具有一定的參考價值,感興趣的可以了解一下2024-03-03

