亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

python線程中join()的用途是什么

python線程中join()的用途是什么

一只斗牛犬 2019-12-06 11:06:09
我正在研究python線程并遇到了join()。作者告訴我,如果線程處于守護程序模式,那么我需要使用它,join()以便線程可以在主線程終止之前完成自身。但我也看到他用t.join(),即使t不daemon示例代碼是這個import threadingimport timeimport logginglogging.basicConfig(level=logging.DEBUG,                    format='(%(threadName)-10s) %(message)s',                    )def daemon():    logging.debug('Starting')    time.sleep(2)    logging.debug('Exiting')d = threading.Thread(name='daemon', target=daemon)d.setDaemon(True)def non_daemon():    logging.debug('Starting')    logging.debug('Exiting')t = threading.Thread(name='non-daemon', target=non_daemon)d.start()t.start()d.join()t.join()我不知道這是什么用途,t.join()因為它不是守護程序,即使刪除它也看不到任何變化
查看完整描述

4 回答

?
神不在的星期二

TA貢獻1963條經驗 獲得超6個贊

展示這種機制的技術有些笨拙:join()大概是由主線程調用的。也可以由另一個線程調用它,但是會不必要地使該圖復雜化。


join調用應該放在主線程的軌道上,但是為了表達線程關系并使它盡可能簡單,我選擇將其放在子線程中。


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

因此,您看不到任何更改的原因是因為您的主線程在之后沒有執行任何操作join。您可以說join(僅)與主線程的執行流程相關。


例如,如果您要同時下載一堆頁面以將它們串聯成一個大頁面,則可以使用線程開始并發下載,但是需要等到最后一頁/線程完成后才能開始組裝單個頁面在很多。那是您使用的時間join()。



查看完整回答
反對 回復 2019-12-07
?
小怪獸愛吃肉

TA貢獻1852條經驗 獲得超1個贊

直接來自文檔


join([timeout])等待線程終止。這將阻塞調用線程,直到被調用join()方法的線程終止(正?;蛲ㄟ^未處理的異常終止),或直到發生可選的超時為止。


這意味著,滋生主線程t和d,等待t完成,直到它完成。


根據您的程序采用的邏輯,您可能要等到線程完成后再繼續主線程。


另外從文檔:


線程可以標記為“守護程序線程”。該標志的重要性在于,僅保留守護程序線程時,整個Python程序都會退出。


一個簡單的例子,說我們有這個:


def non_daemon():

    time.sleep(5)

    print 'Test non-daemon'


t = threading.Thread(name='non-daemon', target=non_daemon)


t.start()

結束于:


print 'Test one'

t.join()

print 'Test two'

這將輸出:


Test one

Test non-daemon

Test two

在這里,主線程顯式等待t線程完成,直到print第二次調用為止。


或者,如果我們有這個:


print 'Test one'

print 'Test two'

t.join()

我們將得到以下輸出:


Test one

Test two

Test non-daemon

在這里,我們在主線程中完成工作,然后等待t線程完成。在這種情況下,我們甚至可以刪除顯式連接t.join(),并且程序將隱式等待t完成。



查看完整回答
反對 回復 2019-12-07
?
胡說叔叔

TA貢獻1804條經驗 獲得超8個贊

感謝您提供此主題-它對我也有很大幫助。


我今天學到了有關.join()的知識。


這些線程并行運行:


d.start()

t.start()

d.join()

t.join()

這些依次運行(不是我想要的):


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()

這可行!但是它順序運行。我可以將self.start()放在__ init __中,但是不能將self.join()放入。必須在每個線程啟動后執行此操作。


join()是導致主線程等待線程完成的原因。否則,您的線程將獨自運行。


因此,有一種方法可以將join()視為主線程上的“保留”對象-它可以對線程進行解線程,并在主線程可以繼續之前在主線程中按順序執行。它可以確保您的線程在主線程向前移動之前是完整的。請注意,這意味著在調用join()之前線程已經完成是可以的-在調用join()時立即立即釋放主線程。


實際上,我現在剛想到,主線程在d.join()上等待,直到線程d完成,然后才移至t.join()。


實際上,非常清楚,請考慮以下代碼:


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"

它產生此輸出(請注意print語句如何彼此穿線。)


$ 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()正在阻止主線程。所有三個線程在t1.join()完成之前完成,并且主線程繼續執行打印,然后執行t2.join()然后打印,然后執行t3.join()然后打印。


歡迎更正。我也是線程新手。


(注意:如果您有興趣,我正在為DrinkBot編寫代碼,并且我需要線程來同時運行配料泵,而不是順序運行-等待每個飲料的時間更少。)



查看完整回答
反對 回復 2019-12-07
  • 4 回答
  • 0 關注
  • 314 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號