當(dāng)前位置:
首頁 >
python 线程类 threading.Thread.join() 方法 (自闭,不让别人进来了)
發(fā)布時間:2025/3/19
37
豆豆
生活随笔
收集整理的這篇文章主要介紹了
python 线程类 threading.Thread.join() 方法 (自闭,不让别人进来了)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
def join(self, timeout=None):"""Wait until the thread terminates. 等待線程終止。This blocks the calling thread until the thread whose join() method iscalled terminates -- either normally or through an unhandled exceptionor until the optional timeout occurs.這會阻塞調(diào)用線程,直到其join()方法被調(diào)用的線程終止(正常或通過未處理的異常終止),或直到發(fā)生可選的超時為止。When the timeout argument is present and not None, it should be afloating point number specifying a timeout for the operation in seconds(or fractions thereof). As join() always returns None, you must callisAlive() after join() to decide whether a timeout happened -- if thethread is still alive, the join() call timed out.如果存在timeout參數(shù)而不是None,則它應(yīng)該是一個浮點數(shù),以秒(或其分?jǐn)?shù))指定操作的超時時間。 由于join()始終返回None,因此必須在join()之后調(diào)用isAlive()來確定是否發(fā)生超時-如果線程仍處于活動狀態(tài),則join()調(diào)用將超時。When the timeout argument is not present or None, the operation willblock until the thread terminates.當(dāng)不存在超時參數(shù)或timeout==None,該操作將阻塞直到線程終止。A thread can be join()ed many times.一個線程可以被多次join()。join() raises a RuntimeError if an attempt is made to join the currentthread as that would cause a deadlock. It is also an error to join() athread before it has been started and attempts to do so raises the sameexception.如果嘗試加入當(dāng)前線程,join()可能會引發(fā)RuntimeError,因為可能導(dǎo)致死鎖。 在啟動線程之前join()一個線程也是錯誤的,嘗試這樣做會引發(fā)相同的異常。"""if not self._initialized:raise RuntimeError("Thread.__init__() not called")if not self._started.is_set():raise RuntimeError("cannot join thread before it is started")if self is current_thread():raise RuntimeError("cannot join current thread")if timeout is None:self._wait_for_tstate_lock()else:# the behavior of a negative timeout isn't documented, but# historically .join(timeout=x) for x<0 has acted as if timeout=0# 沒有記錄負(fù)超時的行為,但是歷史上.x(0)的.join(timeout = x)表現(xiàn)為超時= 0self._wait_for_tstate_lock(timeout=max(timeout, 0))
總結(jié)
以上是生活随笔為你收集整理的python 线程类 threading.Thread.join() 方法 (自闭,不让别人进来了)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python 多线程 线程如何获得返回值
- 下一篇: python 线程类 threading