如何让父线程等待指定时间或直到子线程完成?
现在,我的父线程启动了一个子线程,然后继续执行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 个回答
6
给join()函数设置一个60秒的超时时间:
thread.join(60)
在这个调用返回之后,你可以通过isAlive()
来检查这个线程是成功加入了,还是因为超时而没有加入。