Is my thread safe from premature garbage collection?

Benjamin Niemann pink at odahoda.de
Thu Sep 1 05:24:47 EDT 2005


NutJob at gmx.net wrote:

> Hello all,
> 
> I'm aware that in Python an object is cleared for garbage collection as
> soon as the last reference to it disappears. Normally this is fine.
> 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? Or
> is the Python GC smart enough to leave thread objects alone until their
> run() methods have finished?
> 
> If not, I do have a workaround, but it is a bit clumsy IMO. Basically I
> would just keep a list into which each thread object enters a reference
> to itself on creation. This way I'd ensure that I have a reference to
> the thread to prevent the GC from killing it. Then, when a thread is
> about to finish its run() method, the thread finds and removes that
> reference to itself from my list of thread references.
> 
> Anyway, if anyone could make a definite statement on whether threads
> are safe from unwanted garbage collection, that'd be really great.
> Thanks in advance for any helpful replies!

The threading module does already take care of keeping references to all
running threads, so there's no need to do it yourself and your threads are
safe from being garbage collected.

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



More information about the Python-list mailing list