简体   繁体   English

如何等待线程列表完成? (Python)

[英]How to wait for a list of threads to finish? (Python)

For a single thread, I would do对于一个线程,我会做

thread.join(60)
threadEvent.set()

How can I make it wait for all threads in the list to finish OR wait until the timeout and then issue the set() to the threading.Event()?如何让它等待列表中的所有线程完成或等待超时,然后将 set() 发送到 threading.Event()? Each thread has a corresponding threading.Event() that will be used to stop the thread.每个线程都有一个对应的 threading.Event() 用于停止线程。 The important part is that if the timeout is reached, I need to set the event for each thread.重要的部分是,如果达到超时,我需要为每个线程设置事件。

import time
endTime = time.time() + 60
for t in threads:
    t.join(endTime - time.time())
    t.event.set()

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

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