Re: at-exit-thread



On Feb 29, 2:12 pm, castiro...@xxxxxxxxx wrote:
On Feb 29, 1:55 pm, "Diez B. Roggisch" <de...@xxxxxxxxxxxxx> wrote:





castiro...@xxxxxxxxx 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.- Hide quoted text -

Two snippets from the docs that are worrisome:

# If they invoke anything in threading.py that calls currentThread(),
they
# leave an entry in the _active dict forever after.
-threading.py

thread.get_ident()
... Thread identifiers may be recycled when a thread exits and another
thread is created.
-thread module doc

If you try to look up something by thread_id, you can get a terminated
thread's old resources. at-thread-exit could prevent it. What is the
scope of a resource a daemon thread allocates?

.