guettli
guettli

Reputation: 27969

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?

Upvotes: 13

Views: 7992

Answers (2)

guettli
guettli

Reputation: 27969

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

Upvotes: 10

freakish
freakish

Reputation: 56517

This is a code from threading.py module:

import sys as _sys

class Thread(_Verbose):
    def _bootstrap_inner(self):
        # some code

            # If sys.stderr is no more (most likely from interpreter
            # shutdown) use self._stderr.  Otherwise still use sys (as in
            # _sys) in case sys.stderr was redefined since the creation of
            # self.
            if _sys:
               _sys.stderr.write("Exception in thread %s:\n%s\n" % 
                   (self.name, _format_exc()))
            else:
               # some code

might be helpful. The error you see comes from else statement. So in your case:

import sys as _sys

while True:
    if not _sys:
        break/return/die/whatever
    do_something()
    time.sleep(interval)

I'm not sure if it works though (note that interpreter shutdown may happen inside do_something so you should probably wrap everything with try:except:).

Daemon threads are not necessarily bad, they can definitely speed up development process. You just have to be careful with them.

Upvotes: 0

Related Questions