本文實例講述了Python中threading模塊join函數用法。分享給大家供大家參考。具體分析如下:
join的作用是眾所周知的,阻塞進程直到線程執行完畢。通用的做法是我們啟動一批線程,最后join這些線程結束,例如:
for i in range(10): t = ThreadTest(i) thread_arr.append(t)for i in range(10): thread_arr[i].start()for i in range(10): thread_arr[i].join()
此處join的原理就是依次檢驗線程池中的線程是否結束,沒有結束就阻塞直到線程結束,如果結束則跳轉執行下一個線程的join函數。
而py的join函數還有一個特殊的功能就是可以設置超時,如下:
Thread.join([timeout])
Wait until the thread terminates. This blocks the calling thread until the thread whose join() method is called terminates 主站蜘蛛池模板: 红桥区| 江城| 临城县| 新巴尔虎左旗| 绍兴县| 北安市| 禄劝| 和顺县| 余干县| 新沂市| 江西省| 青州市| 夏河县| 陇南市| 邻水| 固镇县| 凤庆县| 资中县| 平泉县| 长沙县| 沙雅县| 赤壁市| 华亭县| 宣威市| 西和县| 天津市| 隆德县| 九龙城区| 墨竹工卡县| 邹平县| 广东省| 崇礼县| 谢通门县| 仙桃市| 榆中县| 团风县| 南阳市| 蓬溪县| 子长县| 新邵县| 平昌县|