我有一个我希望在threading.Event
上重复运行的功能。但是,您似乎只能一次使用每个Event
实例。
有没有办法重置thread.Event
以便可以再次触发?我想它看起来像这样:
import threading
import time
def waiting_function(trigger):
while True:
trigger.wait()
# do stuff after trigger
trigger.reset()
trigger = threading.Event()
waiting_thread = threading.Thread(target=waiting_function, args=[trigger])
waiting_thread.start()
time.sleep(3.) # do some stuff that takes a while
trigger.set()
time.sleep(3.) # do some stuff that takes a while
if not trigger.is_set:
trigger.set()
或者,我是否必须在每个集合之后创建一个新的trigger
并在线程之间共享它?