Is my thread safe from premature garbage collection?

N

NutJob

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
 
F

fraca7

(e-mail address removed) 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.
 
B

Benjamin Niemann

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.
 
S

Sion Arrowsmith

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?
 
B

Benjamin Niemann

Sion said:
[QUOTE="Benjamin Niemann said:
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?[/QUOTE]

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.
 
N

NutJob

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. :p
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top