[docs] [issue13077] Unclear behavior of daemon threads on main thread exit

etuardu report at bugs.python.org
Sat Oct 1 15:43:59 CEST 2011


etuardu <edonan at gmail.com> added the comment:

Let me put it this way: the definition of daemon thread describes the behaviour of the Python program running it (its exit condition in particular) instead of going straight to the point describing the behaviour of the daemon thread itself first, and finally add other considerations.

Specifically, I think a situation like the following is not quite clear from the given definition:
- I have a main thread and a secondary thread, both printing to stdout.
- At some point, I press Ctrl+c raising an unhandled KeyboardInterrupt exception in the main thread, which kills it.

This is what I get using a daemon thread:

etuardu at subranu:~/Desktop$ python foo.py # other = daemon
other thread
main thread
other thread
main thread
^C
Traceback [...]
KeyboardInterrupt
etuardu at subranu:~/Desktop$ # process terminates

This is what I get using a non-daemon thread:

etuardu at subranu:~/Desktop$ python foo.py # other = non-daemon
other thread
main thread
other thread
main thread
^C
Traceback [...]
KeyboardInterrupt
other thread
other thread
other thread
... (process still running)

So, to explain the significance of the "daemon" flag, I'd say something like:

A daemon thread is shut down when the main thread and all others non-daemon threads end.
This means a Python program runs as long as non-daemon threads, such as the main thread, are running.
When only daemon threads are left, the Python program exits.

Of course this can be understood from the current definition («the entire Python program exits when only daemon threads are left»), still it looks a bit sybilline to me.

----------
Added file: http://bugs.python.org/file23283/foo.py

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue13077>
_______________________________________


More information about the docs mailing list