at-exit-thread

castironpi at gmail.com castironpi at gmail.com
Fri Feb 29 15:12:13 EST 2008


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.



More information about the Python-list mailing list