進程、線程之join方法


def join(self,timeout=None)

  """Wait until the thread terminates """

  This blocks the calling thread until the thread whose join() method is called terminates  -- either normally or through an unhandled

      exception or until the optional timeout occurs.

  這將阻止調用進程直到調用join()方法的進程終止--正常 或者 拋出一個非處理的異常 或者 直到 可選 的參數 timeout發生.

  When the timeout argument is present and not None, it should be a floating point number specifying a timeout for the operation in seconds (or fractions                     thereof). As join() always returns None, you must call isAlive() after join() to decide whether a timeout happened -- if the thread is still alive, the join() call                 timeout.

  當timeout參數存在且不是None時,它應該是一個浮點數指明操作的超時時間(以秒為單位)。 join()方法總是返回None,你必須在調用 join() 方法之后 調用  isAlive()方法 決定超時是否發生--如果線程仍處於活動狀態,說明 join()調用超時。

 

  When the timeout argument is not present or None, the operation will block until the thread terminates.

  A thread can be join()ed many times.

  join() raises a RuntimeError if an attempt is made to join the cureent thread as that would cause a deadblock.

  It is also an error to join() a thread  before it has been started and attempts to do so raises the same exception.

  如果嘗試 join 當前線程,join()方法會拋出 運行時錯誤異常 ,因為那樣會導致死鎖。 在一個線程開始調用start()方法之前,調用join()方法,也會拋出同樣的異常。

 

 

import time
from threading import Timer,Thread

def func():
    print(time.strftime("%Y-%m-%d %H:%M:%S"))
    # timer.join() # RuntimeError: cannot join current thread 如果嘗試加入當前線程 join()方法,會拋出運行時錯誤 異常,因為這會導致死鎖
    print("執行我啦")

if __name__ == '__main__':
    print(time.strftime("%Y-%m-%d %H:%M:%S"))
    timer = Timer(1,func)  # 當創建線程后,延時1秒后執行
    # timer.join()   # RuntimeError: cannot join thread before it is started  在線程創建啟動之前,運行join()方法,拋出 運行時錯誤 異常
    timer.start()  # 創建線程,並執行線程
    timer.join()
    print(time.strftime("%Y-%m-%d %H:%M:%S"))

 


免責聲明!

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



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