欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python線程join方法原理解析

 更新時(shí)間:2020年02月11日 10:19:46   作者:xushukui  
這篇文章主要介紹了python線程join方法原理解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

這篇文章主要介紹了python線程join方法原理解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

幾個(gè)事實(shí)

1 python 默認(rèn)參數(shù)創(chuàng)建線程后,不管主線程是否執(zhí)行完畢,都會(huì)等待子線程執(zhí)行完畢才一起退出,有無join結(jié)果一樣

2 如果創(chuàng)建線程,并且設(shè)置了daemon為true,即thread.setDaemon(True), 則主線程執(zhí)行完畢后自動(dòng)退出,不會(huì)等待子線程的執(zhí)行結(jié)果。而且隨著主線程退出,子線程也消亡。

3 join方法的作用是阻塞,等待子線程結(jié)束,join方法有一個(gè)參數(shù)是timeout,即如果主線程等待timeout,子線程還沒有結(jié)束,則主線程強(qiáng)制結(jié)束子線程。

4 如果線程daemon屬性為False, 則join里的timeout參數(shù)無效。主線程會(huì)一直等待子線程結(jié)束。

5 如果線程daemon屬性為True, 則join里的timeout參數(shù)是有效的, 主線程會(huì)等待timeout時(shí)間后,結(jié)束子線程。此處有一個(gè)坑,即如果同時(shí)有N個(gè)子線程join(timeout),那么實(shí)際上主線程會(huì)等待的超時(shí)時(shí)間最長為 N * timeout, 因?yàn)槊總€(gè)子線程的超時(shí)開始時(shí)刻是上一個(gè)子線程超時(shí)結(jié)束的時(shí)刻。

測試代碼

import threading,time

def func():
  print "start thread time: ",time.strftime('%H:%M:%S')
  time.sleep(3)
  print "stop thread time: ",time.strftime('%H:%M:%S')

thread_list = []
for i in range(3):
  t1 = threading.Thread(target=func)
  #t1.setDaemon(True)

  thread_list.append(t1)

for r in thread_list:
  r.start()

for t in thread_list:
  #t.join(1)
  t.join()
print "stop main thread"

###子線程如果設(shè)置了t.join(timeout),則根據(jù)timeout的不同,結(jié)果會(huì)不同,前提是設(shè)置了setDaemon(True),否則join的timeout是沒效的

#設(shè)置了setDaemon(True),但是沒設(shè)置t.join()的運(yùn)行結(jié)果:
#start thread time: 17:25:29
#start thread time: 17:25:29
#start thread time: 17:25:29
#stop main thread

#加了t1.setDaemon(True),并且設(shè)置了超時(shí)時(shí)間t.join(1)的運(yùn)行結(jié)果:
#start thread time: 17:12:24
#start thread time: 17:12:24
#start thread time: 17:12:24
#stop main thread

#沒加t1.setDaemon(True),并且設(shè)置了超時(shí)時(shí)間t.join(1)的運(yùn)行結(jié)果,不過因?yàn)閟etDaemon的參數(shù)不是True所以就算設(shè)置了超時(shí)時(shí)間也沒用:
#start thread time: 17:13:28
#start thread time: 17:13:28
#start thread time: 17:13:28
#stop main thread
#stop thread time:  17:13:31
#stop thread time:  17:13:31
#stop thread time:  17:13:31

#沒加t1.setDaemon(True),但是設(shè)置了t.join(),沒有超時(shí)時(shí)間的阻塞的運(yùn)行結(jié)果:
#start thread time: 17:16:12
#start thread time: 17:16:12
#start thread time: 17:16:12
#stop thread time:  17:16:15
#stop thread time:  17:16:15
#stop thread time:  17:16:15
#stop main thread 

#即沒有設(shè)置setDaemon(True),也沒有設(shè)置join()的運(yùn)行結(jié)果:
#start thread time: 17:22:25
#start thread time: 17:22:25
#start thread time: 17:22:25
#stop main thread
#stop thread time:  17:22:28
#stop thread time:  17:22:28
#stop thread time:  17:22:28

總結(jié):

如果想讓子進(jìn)程正常的運(yùn)行結(jié)束(子進(jìn)程中所有的內(nèi)容都運(yùn)行了),則如果設(shè)置join(timeout)的話,前提是設(shè)置setDaemon(True),且setDaemon的參數(shù)為True,且join(timeout)的超時(shí)時(shí)間必須大于子進(jìn)程執(zhí)行所需的時(shí)間,不然沒等子進(jìn)程運(yùn)行結(jié)束就超時(shí)退出了或者直接設(shè)置join()不帶超時(shí)時(shí)間,也不用設(shè)置setDaemon(True)了

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論