python 主线程_阻止主线程直到python后台线程完成side-task

我有一个线程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(持续时间),除非它只需要在后台线程实际注册时间到时才精确返回.我应该使用其他同步对象或其他策略吗?

我也愿意接受这样的论点,即如果他们是一个好的论据,那么我正以错误的方式接近这一切.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值