我有一个线程python应用程序,后台线程中有一个长时间运行的mainloop.这个后台主循环实际上是对pyglet.app.run()的调用,它驱动一个GUI窗口,也可以配置为定期调用其他代码.我需要从主线程中随意调用do_stuff(duration)函数来触发GUI中的动画,等待动画停止,然后返回.实际动画必须在后台线程中完成,因为GUI库无法处理由单独的线程驱动.
我相信我需要做这样的事情:
import threading
class StuffDoer(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.max_n_times = 0
self.total_n_times = 0
self.paused_ev = threading.Event()
def run(self):
# this part is outside of my control
while True:
self._do_stuff()
# do other stuff
def _do_stuff(self):
# this part is under my control
if self.paused_ev.is_set():
if self.max_n_times > self.total_n_times:
self.paused_ev.clear()
else:
if self.total_n_times >= self.max_n_times:
self.paused_ev.set()
if not self.paused_ev.is_set():
# do stuff that must execute in the background thread
self.total_n_times += 1
sd = StuffDoer()
sd.start()
def do_stuff(n_times):
sd.max_n_times += n_times
sd.paused_ev.wait_for_clear() # wait_for_clear() does not exist
sd.paused_ev.wait()
assert (sd.total_n_times == sd.max_n_times)
编辑:使用max_n_times而不是stop_time来阐明为什么Thread.join(持续时间)不会这样做.
wait([timeout])
Block until the internal flag is true.
If the internal flag is true on entry,
return immediately. Otherwise, block
until another thread calls set() to
set the flag to true, or until the
optional timeout occurs.
我发现如果我有一对事件paused_ev和not_paused_ev,并且使用not_paused_ev.wait(),我可以得到我正在寻找的行为.我几乎只能使用Thread.join(持续时间),除非它只需要在后台线程实际注册时间到时才精确返回.我应该使用其他同步对象或其他策略吗?
我也愿意接受这样的论点,即如果他们是一个好的论据,那么我正以错误的方式接近这一切.