When is a thread garbage collected?

Discussion in 'Python' started by Kent Johnson, Mar 16, 2005.

  1. Kent Johnson

    Kent Johnson Guest

    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
     
    Kent Johnson, Mar 16, 2005
    #1
    1. Advertising

  2. 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
     
    =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=, Mar 16, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Sg==?=

    When are static members garbage collected?

    =?Utf-8?B?Sg==?=, Feb 25, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    345
    bruce barker
    Feb 25, 2004
  2. Cheng Thao
    Replies:
    0
    Views:
    390
    Cheng Thao
    Aug 6, 2003
  3. Mark McKay
    Replies:
    5
    Views:
    879
    xarax
    Oct 3, 2003
  4. Scott Robinson

    Socket being garbage collected too early

    Scott Robinson, Dec 16, 2004, in forum: Python
    Replies:
    4
    Views:
    414
    Scott Robinson
    Dec 18, 2004
  5. Daniel
    Replies:
    21
    Views:
    664
    Roedy Green
    Oct 23, 2007
Loading...

Share This Page