2011-07-06 44 views
1

現在,我的父線程啓動子線程,然後進入time.sleep()一段時間。有什麼辦法可以讓我的父線程睡覺或thread.join()? (如果我沒記錯的Thread.join()是等待子線程完成一個)如何使父線程等待指定的時間或直到子線程完成?

thread = threading.Thread(target=whatever, args=yeah) 
thread.start() 
#here wait till either 60 seconds has passed or the child thread finishes, which ever comes first 
#if 60 passes stop child thread (I already have a way to do this) 
#continue doing other stuff 

回答

6

通過60秒的超時給join()函數:

thread.join(60) 

後該呼叫已返回,您可以根據isAlive()呼叫檢查線程是否加入或超時。