如何让父线程等待指定的时间或直到子线程完成?

时间:2011-07-06 20:13:56

标签: python multithreading

现在,我的父线程启动子线程,然后进入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

1 个答案:

答案 0 :(得分:6)

将60秒超时传递给join()函数:

thread.join(60)

在该呼叫返回后,您可以根据isAlive()电话检查线程是否已加入或超时。