Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Is my thread safe from premature garbage collection?

Reply
Thread Tools

Is my thread safe from premature garbage collection?

 
 
NutJob@gmx.net
Guest
Posts: n/a
 
      09-01-2005
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!

Cheers,

antred

 
Reply With Quote
 
 
 
 
fraca7
Guest
Posts: n/a
 
      09-01-2005
a écrit :

> 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!


As far as I know, the threading module keeps a reference around for each
thread, until its target method returns. I frequently use a thread
without keeping any reference to it and never encountered any problem.
 
Reply With Quote
 
 
 
 
Benjamin Niemann
Guest
Posts: n/a
 
      09-01-2005
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/
 
Reply With Quote
 
NutJob@gmx.net
Guest
Posts: n/a
 
      09-01-2005
Splendid! =) Thanks guys!

 
Reply With Quote
 
Sion Arrowsmith
Guest
Posts: n/a
 
      09-01-2005
In article <df6h86$73b$>, Benjamin Niemann <> wrote:
> 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?

--
\S -- -- http://www.chaos.org.uk/~sion/
___ | "Frankly I have no feelings towards penguins one way or the other"
\X/ | -- Arthur C. Clarke
her nu becomež se bera eadward ofdun hlęddre heafdes bęce bump bump bump
 
Reply With Quote
 
Benjamin Niemann
Guest
Posts: n/a
 
      09-01-2005
Sion Arrowsmith wrote:

> In article <df6h86$73b$>, Benjamin Niemann <>
> wrote:
>> 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/
 
Reply With Quote
 
NutJob@gmx.net
Guest
Posts: n/a
 
      09-08-2005
Good point there. Sorry, I should have thought of that myself really,
but well, I guess I'm having my senior moments a bit early.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
html5lib not thread safe. Is the Python SAX library thread-safe? John Nagle Python 5 03-12-2012 04:07 PM
os.ChDir() not thread-safe; was : Is tempfile.mkdtemp() thread-safe? Gabriel Rossetti Python 0 08-29-2008 08:30 AM
Premature Piece of Garbage Marshalx Windows 64bit 65 06-10-2006 09:05 PM
Safe garbage collecting Robert Potthast C++ 10 09-05-2005 03:25 PM
Templates - Garbage In Garbage Not Out ramiro_b@yahoo.com C++ 1 07-25-2005 04:48 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57