[Tutor] Threads

Tim Peters tim.peters at gmail.com
Tue Nov 16 04:06:51 CET 2004

[Terry Carroll]
[Terry Carroll]
>>> .... I want the app to exit, and the threads it started to go away;
> > > they're done.

[Tim Peters]
>> That's probably what *will* happen [depending on OS].
> If that's what happens, great.  I just don't want either the threads to
> stay around (if I make them daemons) or for the application to hang
> rather than exit.
> I suppose I could just write an app that starts a thread and exits
> without shutting it down, to see if it hangs, huh?

If you're talking about a threading.Thread, it will hang unless you
marked the thread as a daemon thread.  If you're talking about a "raw
thread" created by the low-level thread module, don't <wink> -- use
threading.Thread instead.

If you do mark a threading.Thread as daemonic, the program will not
hang at exit regardless of whether the OS kills the thread.

To determine whether your OS kills a daemon thread when Python exits,
try something like this:

import threading
import time

class Worker(threading.Thread):
    def run(self):
        while True:
            print "thread still running"

t = Worker()
time.sleep(3)  # let the thread get started
print "main thread going away now"

If you see an unending parade of "thread still running" lines after
you see "main thread going away now", then your OS is not killing the

More information about the Tutor mailing list