问题描述
我们被这个错误击中了
http://bugs.python.org/issue1856 守护程序在解释器关闭期间线程段错误. /p>
现在,我正在寻找一种解决此错误的方法.
此刻代码如下:
while True:
do_something()
time.sleep(interval)
有没有办法在do_something()之前检查解释器是否仍然可用?
还是最好不要执行mythread.setDaemon(True)并检查主线程是否已退出?
回答自己的问题:
我现在使用这种模式:不要setDaemon(True),不要使用sleep(),使用parent_thread.join()
while True:
parent_thread.join(interval)
if not parent_thread.is_alive():
break
do_something()
相关: http://docs.python.org/2/library/threading .html#threading.Thread.join
We were hit by this bug:
http://bugs.python.org/issue1856 Daemon threads segfault during interpreter shut down.
Now I search a way to code around this bug.
At the moment the code looks like this:
while True:
do_something()
time.sleep(interval)
Is there a way to check if the interpreter is still usable before do_something()?
Or is it better to not do mythread.setDaemon(True) and the check if the main thread has exited?
Answer to own question:
I use this pattern now: don't setDaemon(True), don't use sleep(), use parent_thread.join()
while True:
parent_thread.join(interval)
if not parent_thread.is_alive():
break
do_something()
Related: http://docs.python.org/2/library/threading.html#threading.Thread.join
这篇关于检测守护程序线程中关闭的解释器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!