Skip to main content
 首页 » 编程设计

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

2025年05月04日18leader

现在,我的父线程启动子线程,然后继续执行 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() 函数:

thread.join(60) 

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