Garbage collector and threads

Nicolas Fleury nid_oizo at yahoo.com_removethe_
Mon Mar 1 22:01:48 EST 2004


Aahz wrote:
> In article <R4M0c.4365$mx4.107234 at nnrp1.uunet.ca>,
> Nicolas Fleury  <nid_oizo at yahoo.com_remove_the_> wrote:
>>But even if I use "thread", I have no way to put the functionality in 
>>one class if I want access the members in the running thread.
> 
> Huh?  I don't understand what you mean.

Well, suppose I want to have a class with the same interface of 
threading.Thread, but with two additions:
class CancellableThread:
     def __del__(self): self.cancel()

The first addition is that the finalizer use a sub-class implemented 
mechanism for cancellation of the thread.
The second addition is that, as soon as the object is not referred 
anymore in the parent thread, the thread object is finalized.

Note that it doesn't need to be the finalizer, what is necessary is 
something to be called when the object is no more referred in the parent 
thread.

I'm saying that I don't know of any way to do that with a single class, 
because I expect the sub-class to define something like:
     def run(self)
And the thread object will be referred in the running thread by "self", 
so I'm stuck without any way to know when the thread object is not 
referred in the main thread.

I can do something like:
class Canceller:
     def __init__(self, thread): self.thread = thread
     def __del__(self): self.thread.cancel()

so that I encapsulate all threads with "Cancellers" in the parent 
thread, but then I have to use two objects to the job that could 
possibly be done by one.

>>- Parent threads need to be cancelled before their children threads.
> 
> Why's that a problem?  Just hold references to the parent threads in the
> main thread and cascade the cancels down.

You're right, it works.  At the moment I wrote that I expected mix of a 
CancellableThread class implemented with this mechanism with other 
threads classes to cause loss of link between threads.  If that happens 
all threads that are not CancellableThreads (like the main thread) must 
call the function to cancel remaining CancellableThreads started 
directly by them.

>>- It only works at the end of the main thread; it cannot be used at the 
>>middle of an appplication.
> 
> Why?

Because the goal is to cancel threads that are no more referred in the 
main thread.  Since there's no way to know which they are, there's no 
way to do it at the middle of an application.  You have to do it at the 
end of the main thread, as for other threads, where you can assumed that 
cancellable threads should be cancelled and joined.

I don't know if my explications of what I'm searching are more clear.  I 
have the feeling it's impossible to do it without a language extension 
and at the same time that it would be less error-prone than 
threading.Thread as deamons or not.  Maybe I could write a PEP just for 
the exercise of it...

Regards,
Nicolas



More information about the Python-list mailing list