at-exit-thread

Diez B. Roggisch deets at nospam.web.de
Sat Mar 1 12:13:59 EST 2008


castironpi at gmail.com schrieb:
> On Feb 29, 1:55 pm, "Diez B. Roggisch" <de... at nospam.web.de> wrote:
>> castiro... at gmail.com schrieb:
>>
>>> The Python main interpreter has an at-exit list of callables, which
>>> are called when the interpreter exits.  Can threads have one?  What's
>>> involved, or is the best way merely to subclass Thread?
>> Is that some sort of trick-question?
>>
>> class MyThread(Thread):
>>
>>     def run(self):
>>         while some_condition:
>>              do_something()
>>         do_something_after_the_thread_ends()
>>
>> The atexit stuff is for process-termination which is/may be induced by
>> external signals - which is the reason why these callbacks extist.
>> Threads don't have that, thus no need.
> 
> That depends.  If a thread adds an object it creates to a nonlocal
> collection, such as a class-static set, does it have to maintain a
> list of all such objects, just to get the right ones destroyed on
> completion?  Processes destroy their garbage hassle-free; how can
> threads?  And don't forget Thread.run( self ) in the example, if
> anyone ever wants to make use of the 'target' keyword.

That is both totally unrelated to the original question and plain wrong. 
Processes don't destroy their garbage themselves, not automagically or 
anything. If you _want_, you can put atexit-handlers that do clean up 
some stuff, but you'll have to equip them with the necessary context 
yourself.

And a

kill -9 <pid>

won't run *anything* of your own code.

The OS is eventually responsible for cleaning up.

Diez



More information about the Python-list mailing list