[Tutor] Threads

Tim Peters tim.peters at gmail.com
Tue Nov 16 03:21:29 CET 2004


[Kent Johnson]
>> If the threads are marked as daemon threads (call
>> thread.setDaemon(True)) then Python will exit even if the thread is
>> still alive.

[Terry Carroll]
> That's what I thought.  That's the opposite of what I want to have
> happen.  I want the app to exit, and the threads it started to go away;
> they're done.

That's probably what *will* happen.  Since Python exits in this case,
it has no more say about what the threads do.  Whether the threads die
too is then up to the platform (operating system + thread libraries). 
Under most OSes, when the main thread goes away, child threads are
automatically killed too.  There's a bit about this in the Python docs
for the thread module:

    When the main thread exits, it is system defined whether the other
    threads survive. On SGI IRIX using the native thread implementation,
    they survive. On most other systems, they are killed without
    executing try ... finally clauses or executing object destructors.

But even if you can get away with it, it's generally a better idea to
do a controlled shutdown yourself.  For example, some day you're going
to want those threads to write a summary of what they accomplished to
a log file -- or something.  It's *always* something <wink>.


More information about the Tutor mailing list