Garbage collector and threads

Nicolas Fleury nid_oizo at yahoo.com_remove_the_
Mon Mar 1 14:23:35 EST 2004


Aahz wrote:
> As Michael Hudson said, it doesn't work that way in Python, because all
> Python objects are global (and I mean truly global here, not just module
> global).  Essentially, all objects are heap objects that can be traced
> by crawling through Python's internals.

I understand that.  RAII is often used when speaking of scoped 
variables.  In my case I was talking about global scope in main thread.

> Right.  As I said, you have to use the ``thread`` module directly; you
> can't use ``threading``.

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.

>>I just want a way to garantee that all my threads are cancelled when the 
>>main thread is finished.
> 
> So hold references to them and call the ``cancel()`` method yourself.

That's a possible solution.  Make a custom Thread class with a cancel 
method and with all instances registered somewhere.  At the end of the 
main thread, I call something to cancel all these threads.  There's 
however some problems with that solution:
- Parent threads need to be cancelled before their children threads.
- It only works at the end of the main thread; it cannot be used at the 
middle of an appplication.
- The cancellations are not automatic (I guess there's a way to do it by 
overriding something like exit, anyway...)

All these problems are solved by using RAII with the Canceller class I 
shown in a previous message, so I prefer using this solution.  In fact, 
what I would really need, is a mechanism, and I don't know if there's 
already one, to disable ref-counting of a thread object in its 
corresponding thread, so that I could provide a single class for 
cancellable threads.

Regards,
Nicolas




More information about the Python-list mailing list