繁体   English   中英

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

[英]How to make a parent thread wait either specified amount of time or until the child thread finishes?

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

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

thread.join(60)

在该调用返回后,您可以根据isAlive()调用检查线程是否加入或超时。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM