Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect Interpreter shut down in daemon thread

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?

like image 247
guettli Avatar asked Aug 07 '13 08:08

guettli


1 Answers

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

like image 123
guettli Avatar answered Oct 22 '22 05:10

guettli