3 回答

TA貢獻(xiàn)1825條經(jīng)驗(yàn) 獲得超6個(gè)贊
展示這種機(jī)制的技術(shù)有些笨拙:join()大概是由主線程調(diào)用的。也可以由另一個(gè)線程調(diào)用它,但是會(huì)不必要地使該圖復(fù)雜化。
join調(diào)用應(yīng)該放在主線程的軌道上,但是為了表達(dá)線程關(guān)系并使它盡可能簡(jiǎn)單,我選擇將其放在子線程中。
without join:
+---+---+------------------ main-thread
| |
| +........... child-thread(short)
+.................................. child-thread(long)
with join
+---+---+------------------***********+### main-thread
| | |
| +...........join() | child-thread(short)
+......................join()...... child-thread(long)
with join and daemon thread
+-+--+---+------------------***********+### parent-thread
| | | |
| | +...........join() | child-thread(short)
| +......................join()...... child-thread(long)
+,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, child-thread(long + daemonized)
'-' main-thread/parent-thread/main-program execution
'.' child-thread execution
'#' optional parent-thread execution after join()-blocked parent-thread could
continue
'*' main-thread 'sleeping' in join-method, waiting for child-thread to finish
',' daemonized thread - 'ignores' lifetime of other threads;
terminates when main-programs exits; is normally meant for
join-independent tasks
因此,您看不到任何更改的原因是因?yàn)槟闹骶€程在之后沒有執(zhí)行任何操作join。您可以說(shuō)join(僅)與主線程的執(zhí)行流程相關(guān)。
例如,如果您要同時(shí)下載一堆頁(yè)面以將它們串聯(lián)成一個(gè)大頁(yè)面,則可以使用線程開始并發(fā)下載,但是需要等到最后一頁(yè)/線程完成后才能開始組裝單個(gè)頁(yè)面在很多。那是您使用的時(shí)間join()。

TA貢獻(xiàn)1866條經(jīng)驗(yàn) 獲得超5個(gè)贊
直接來(lái)自文檔
join([timeout])等待線程終止。這將阻塞調(diào)用線程,直到被調(diào)用join()方法的線程終止(正?;蛲ㄟ^未處理的異常終止),或直到發(fā)生可選的超時(shí)為止。
這意味著,滋生主線程t和d,等待t完成,直到它完成。
根據(jù)您的程序采用的邏輯,您可能要等到線程完成后再繼續(xù)主線程。
另外從文檔:
線程可以標(biāo)記為“守護(hù)程序線程”。該標(biāo)志的重要性在于,僅保留守護(hù)程序線程時(shí),整個(gè)Python程序都會(huì)退出。
一個(gè)簡(jiǎn)單的例子,說(shuō)我們有這個(gè):
def non_daemon():
time.sleep(5)
print 'Test non-daemon'
t = threading.Thread(name='non-daemon', target=non_daemon)
t.start()
結(jié)束于:
print 'Test one'
t.join()
print 'Test two'
這將輸出:
Test one
Test non-daemon
Test two
在這里,主線程顯式等待t線程完成,直到print第二次調(diào)用為止。
或者,如果我們有這個(gè):
print 'Test one'
print 'Test two'
t.join()
我們將得到以下輸出:
Test one
Test two
Test non-daemon
在這里,我們?cè)谥骶€程中完成工作,然后等待t線程完成。在這種情況下,我們甚至可以刪除顯式連接t.join(),并且程序?qū)㈦[式等待t完成。

TA貢獻(xiàn)1793條經(jīng)驗(yàn) 獲得超6個(gè)贊
感謝您提供此主題-它對(duì)我也有很大幫助。
我今天學(xué)到了有關(guān).join()的知識(shí)。
這些線程并行運(yùn)行:
d.start()
t.start()
d.join()
t.join()
這些依次運(yùn)行(不是我想要的):
d.start()
d.join()
t.start()
t.join()
特別是,我試圖變得聰明和整潔:
class Kiki(threading.Thread):
def __init__(self, time):
super(Kiki, self).__init__()
self.time = time
self.start()
self.join()
這可行!但是它順序運(yùn)行。我可以將self.start()放在__ init __中,但是不能將self.join()放入。必須在每個(gè)線程啟動(dòng)后執(zhí)行此操作。
join()是導(dǎo)致主線程等待線程完成的原因。否則,您的線程將獨(dú)自運(yùn)行。
因此,有一種方法可以將join()視為主線程上的“保留”對(duì)象-它可以對(duì)線程進(jìn)行解線程,并在主線程可以繼續(xù)之前在主線程中按順序執(zhí)行。它可以確保您的線程在主線程向前移動(dòng)之前是完整的。請(qǐng)注意,這意味著在調(diào)用join()之前線程已經(jīng)完成是可以的-在調(diào)用join()時(shí)立即立即釋放主線程。
實(shí)際上,我現(xiàn)在剛想到,主線程在d.join()上等待,直到線程d完成,然后才移至t.join()。
實(shí)際上,非常清楚,請(qǐng)考慮以下代碼:
import threading
import time
class Kiki(threading.Thread):
def __init__(self, time):
super(Kiki, self).__init__()
self.time = time
self.start()
def run(self):
print self.time, " seconds start!"
for i in range(0,self.time):
time.sleep(1)
print "1 sec of ", self.time
print self.time, " seconds finished!"
t1 = Kiki(3)
t2 = Kiki(2)
t3 = Kiki(1)
t1.join()
print "t1.join() finished"
t2.join()
print "t2.join() finished"
t3.join()
print "t3.join() finished"
它產(chǎn)生此輸出(請(qǐng)注意print語(yǔ)句如何彼此穿線。)
$ python test_thread.py
32 seconds start! seconds start!1
seconds start!
1 sec of 1
1 sec of 1 seconds finished!
21 sec of
3
1 sec of 3
1 sec of 2
2 seconds finished!
1 sec of 3
3 seconds finished!
t1.join() finished
t2.join() finished
t3.join() finished
$
t1.join()正在阻止主線程。所有三個(gè)線程在t1.join()完成之前完成,并且主線程繼續(xù)執(zhí)行打印,然后執(zhí)行t2.join()然后打印,然后執(zhí)行t3.join()然后打印。
歡迎更正。我也是線程新手。
(注意:如果您有興趣,我正在為DrinkBot編寫代碼,并且我需要線程來(lái)同時(shí)運(yùn)行配料泵,而不是順序運(yùn)行-等待每個(gè)飲料的時(shí)間更少。)
添加回答
舉報(bào)