Is my thread safe from premature garbage collection?

Benjamin Niemann pink at odahoda.de
Thu Sep 1 12:34:21 EDT 2005


Sion Arrowsmith wrote:

> In article <df6h86$73b$2 at online.de>, Benjamin Niemann  <pink at odahoda.de>
> wrote:
>>NutJob at gmx.net wrote:
>>> However, in my current project I'm creating a bunch of threads which
>>> are supposed to run until they've completed their run() method, and I'm
>>> worried that if I do not keep references to these thread objects
>>> around, the GC might happily delete them (and thereby kill my thread
>>> routines maybe?) while they're not done yet. Is this fear justified?
>>The threading module does already take care of keeping references to all
>>running threads,
> 
> The implementation of threading.enumerate() would be entertaining if it
> didn't.
> 
> Quite apart from which, I presume the OP's run() method looks something
> like:
> class MyThread(threading.Thread):
>     def run(self):
>         ...
> So what is self if not a reference to the Thread object which is kept
> around until run() has completed?

This was just too obvious;) Looking at the sourcecode of the threading
module and discovering the 'limbo' dict, where every thread stores a
reference to itself, was certainly more entertaining.

-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://www.odahoda.de/



More information about the Python-list mailing list