question about ctrl-d and atexit with threads

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Fri Mar 6 00:27:28 CET 2009


En Thu, 05 Mar 2009 15:26:18 -0200, Darren Dale <dsdale24 at gmail.com>  
escribió:

> On Mar 5, 12:02 pm, s... at pobox.com wrote:
>> What happens if you simply call
>>
>>     my_thread.setDaemon(True)
>>
>> (or in Python 2.6):
>>
>>     my_thread.daemon = True
>>
>> ?  That is the documented way to exit worker threads when you want the
>> application to exit.  From the threading module docs:
>>
>>     "The entire Python program exits when no alive non-daemon threads  
>> are
>>     left."
>
> Thank you Skip, that solves the problem. I'm still curious what the
> difference is between python's handling of sys.exit and EOF, but its
> academic at this point.

Some applications open a new window for each document they're handling.  
When you close the last window, the application exits ("close" does an  
implicit "quit"). Note that this does *not* happen when you close the  
first, original document you opened, but when there are no more documents  
open. The first document is not special in this regard.

Python threads work the same way; a thread may finish, but as long as  
there are other threads alive, the process continues running. Only after  
the last thread has finished, the application quits. The main thread *is*  
special sometimes, but not in this aspect,
Setting daemon=True is like telling Python "I don't care about this  
thread; don't wait for it if that's the only thing you have to do".

Calling sys.exit() is an explicit statement: "I want this program to  
finish now" (or as soon as possible). It doesn't wait for the remaining  
threads (unless you explicitely do so, like in your code).

-- 
Gabriel Genellina




More information about the Python-list mailing list