Garbage collector and threads

Aahz aahz at pythoncraft.com
Mon Mar 1 12:44:46 EST 2004


In article <6uK0c.4347$mx4.106055 at nnrp1.uunet.ca>,
Nicolas Fleury  <nid_oizo at yahoo.com_remove_the_> wrote:
>Aahz wrote:
>>
>> What's "RAII"?
>
>"Resource Acquisition Is Initialization"
>It's a term frequently used in C++ to describe the use of constructor 
>and destructor for resource allocation/deallocation.

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.

>> Perhaps, but threading needs references to created threads in order to
>> implement its functionality.  You might try building your own thread
>> constructs using ``thread`` directly.
>
>I still don't know how to implement it.  I can do something very simple 
>like:
>
>class Canceller:
>     def __init__(self, thread):
>         self.thread = thread
>     def __del__(self):
>         self.thread.cancel()
>         self.thread.join()
>
>but I can't merge the concept with a Thread class, because the self 
>passed to run would be a reference to the object...  I don't know if a 
>language extension would be worth the effort or if there's a cool 
>solution I'm missing, but what is sure is that I write a lot of code to 
>cancel threads in finalizers of parent objects.  I don't want deamon 
>threads so that everything is stopped cleanly, but at the same time it 
>is error-prone in a GUI application, because I risk to have the process 
>still running after a quit.

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

>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.
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

"Do not taunt happy fun for loops. Do not change lists you are looping over."
--Remco Gerlich, comp.lang.python



More information about the Python-list mailing list