检测守护程序线程中关闭的解释器 [英] Detect Interpreter shut down in daemon thread

查看:66
本文介绍了检测守护程序线程中关闭的解释器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们被这个错误击中了

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

这篇关于检测守护程序线程中关闭的解释器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆