En Fri, 29 Feb 2008 18:12:13 -0200, <[EMAIL PROTECTED]> escribió: > On Feb 29, 1:55 pm, "Diez B. Roggisch" <[EMAIL PROTECTED]> wrote: >> [EMAIL PROTECTED] 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?
Yes, like any other objects. All threads in a process share the same memory space; any thread can "see" any other created object, and the general rules on reference counting apply: an object is destroyed when it is no more referenced. There are threading.local objects, which are specially designed to provide per-thread storage; they are not shared among threads. (BTW, you should use a threading.local instance instead of indexing by get_ident()) > 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. Any object created inside a thread will be destroyed when the last reference to it is removed, as any other object. Threads are not special in this regard. -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list