python线程wait_python – 如何知道threading.Condition.wait(timeout)是否已超时或已被通知?...

我正在开发一个带有一些线程的应用程序,每个线程运行一个带有时间睡眠的无限循环.我想要的是在主要完成后完成所有线程,这里是一个例子:

def main():

display_res_stop = threading.Condition()

display_result_t = threading.Thread(target=sample_t, args=(display_res_stop, ))

display_result_t.start()

time.sleep(4)

display_res_stop.acquire()

display_res_stop.notify()

display_res_stop.release()

def sample_t(stop_cond):

stop_cond.acquire()

while True:

print 5

c = stop_cond.wait(10)

stop_cond.release()

if __name__ == '__main__':

main()

这个解决方案的问题是我不知道condition.wait是否已经完成,因为超时或因为已经通知.在第二种情况下,while循环应该完成.

起初我正在做一个time.sleep(t)并使用线程事件但是应用程序必须等到所有线程都经过.

我正在考虑使用threading.Condition和Event的混合解决方案,但我不知道它是否是最好的事情(条件为’sleep’和Event替换为True).

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值