473,397 Members | 1,972 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

When is a thread garbage collected?

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
1 4880
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: David Jenkins | last post by:
I have a very small application designed so that the main thread spawns 3 additional threads. All 4 threads then wait for an event from an Oracle database (message posted on an AQ queue), and are...
4
by: Scott Robinson | last post by:
I have been having trouble with the garbage collector and sockets. Unfortunately, google keeps telling me that the problem is the garbage collector ignoring dead (closed?) sockets instead of...
5
by: Razzie | last post by:
Hi all, A question from someone on a website got me thinking about this, and I wondered if anyone could explain this. A System.Threading.Timer object is garbage collected if it has no...
3
by: cider123 | last post by:
I have a simple Window Service that loads up a list of Folders to monitor, along with parameters for a maintenance profile. Every time the interval is reached, the Window Service spawns off a...
1
by: J | last post by:
I have an object that I am using in my ASP.net app. I only want one instance of this object available to all of my pages. Here is a sample public class statictes private statictest() {} //...
11
by: PJ | last post by:
I'd like to create a subsystem in my asp.net application that is responsible for emails that need to be send out based upon certain events so that the main request/response threads aren't...
14
by: Piotrek | last post by:
Hi all. I have a web app, in which I use frames. My main frameset consists of three inner frames. When some button is pressed in frame A, then content of frame B is reloaded. I am using such...
7
by: Peter Michaux | last post by:
Hi, Below is an example from Flanagan's fifth edition Ajax chapter. It looks to me like the "request" variable might be eligible for garbage collection after this function executes. This would...
11
by: MikeT | last post by:
This may sound very elementary, but can you trap when your object is set to null within the object? I have created a class that registers an event from an object passed in the constructor. When...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.