Python多線程join的用法


 1 import threading, time
 2 def Myjoin():
 3     print 'hello world!'
 4     time.sleep(1)
 5 for i in range(5):
 6     t=threading.Thread(target=Myjoin)
 7     t.start()
 8     t.join()
 9 print 'hello main'
10 #輸出:(每隔一秒輸出)
11 hello world!
12 hello world!
13 hello world!
14 hello world!
15 hello world!
16 hello main
17 注釋掉join
18 #輸出:
19 hello world!
20  hello world!hello world!
21 
22 hello world!
23 hello world!hello main
24 (全部輸出后等待一秒程序結束)

所以join的作用是保證當前線程執行完成后,再執行其它線程。join可以有timeout參數,表示阻塞其它線程timeout秒后,不再阻塞。詳見官方文檔。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM