By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,235 Members | 1,057 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,235 IT Pros & Developers. It's quick & easy.

When is a thread garbage collected?

P: n/a
If I create and start a thread without keeping a reference to the thread, when is the thread garbage
collected?

What I would like is for the thread to run to completion, then be GCed. I can't find anything in the
docs that specifies this behavior; nor can I think of any other behaviour that seems reasonable :-)

For example with this code:

def genpassenger(num):
for i in range(num):
passenger().start()

class passenger(threading.Thread):
def run:
#do something

will all the passenger threads run to completion, then be GCed?

Thanks,
Kent
Jul 18 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Kent Johnson wrote:
If I create and start a thread without keeping a reference to the
thread, when is the thread garbage collected?
When the last reference to the Thread disappears, which is definitely
after the thread terminates.

(Notice that this sentence uses the word thread twice: once to denote
the operating system schedulable unit, and once to denote the Python
object. Only the Python object is subject to garbage collection; the
operating system schedulable unit is not)
For example with this code:

def genpassenger(num):
for i in range(num):
passenger().start()

class passenger(threading.Thread):
def run:
#do something

will all the passenger threads run to completion, then be GCed?


In this example, you'll get a syntax error, because the syntax
for the run method is wrong. If you correct the example, the
answer to the question you asked literally is "yes", but this
is likely not the question you meant to ask. Instead, it might
be that you meant to ask "... then immediately be GCed?" to
which the answer is "it depends on the body of #do something".

IOW, the Thread object may live much longer than the end of the
thread.

Regards,
Martin
Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.