admin管理员组文章数量:1599541
我正在开发一个带有一些线程的应用程序,每个线程运行一个带有时间睡眠的无限循环.我想要的是在主要完成后完成所有线程,这里是一个例子:
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).
本文标签: 已被线程通知waitpythonPython
版权声明:本文标题:python线程wait_python – 如何知道threading.Condition.wait(timeout)是否已超时或已被通知?... 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/dongtai/1728324671a1154280.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论