Is my thread safe from premature garbage collection?

Discussion in 'Python' started by NutJob@gmx.net, Sep 1, 2005.

  1. Guest

    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
    , Sep 1, 2005
    #1
    1. Advertising

  2. fraca7 Guest

    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.
    fraca7, Sep 1, 2005
    #2
    1. Advertising

  3. 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/
    Benjamin Niemann, Sep 1, 2005
    #3
  4. Guest

    Splendid! =) Thanks guys!
    , Sep 1, 2005
    #4
  5. 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
    Sion Arrowsmith, Sep 1, 2005
    #5
  6. 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/
    Benjamin Niemann, Sep 1, 2005
    #6
  7. Guest

    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
    , Sep 8, 2005
    #7
    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. Øyvind Isaksen
    Replies:
    1
    Views:
    965
    Øyvind Isaksen
    May 18, 2007
  2. Replies:
    0
    Views:
    448
  3. Gabriel Rossetti
    Replies:
    0
    Views:
    1,314
    Gabriel Rossetti
    Aug 29, 2008
  4. Peter Laurens
    Replies:
    4
    Views:
    139
    Peter Laurens
    Oct 30, 2007
  5. John Nagle
    Replies:
    5
    Views:
    466
    John Nagle
    Mar 12, 2012
Loading...

Share This Page