Python线程。事件重置?

时间:2017-01-19 23:27:27

标签: python multithreading events

我有一个我希望在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并在线程之间共享它?

0 个答案:

没有答案