How should threads be terminated? (related to 'Help with thread related tracebacks')

Discussion in 'Python' started by Maxwell Hammer, Jun 16, 2005.

  1. Hi all,

    This is related to an earlier post 'Help with thread related
    tracebacks'...for which I have had no feedback yet :-(

    How should a thread complete i.e. how should it exit?
    Reading the python online docs one gets the idea that simply returning is
    OK - but I'm not sure.
    Is it ok to do a sys.ext()? Use 'return' or just let them run out with no
    'return' ???

    thanks.
    Maxwell Hammer, Jun 16, 2005
    #1
    1. Advertising

  2. Maxwell Hammer

    Brian Guest

    Re: How should threads be terminated? (related to 'Help with threadrelated tracebacks')

    Hi Maxwell,

    Yes, to terminate a thread in Python early, use the following:

    import sys; sys.exit(0)

    This kills the particular thread without wiping out the entire Python
    application.

    Hope this helps,

    Brian :)
    ---


    Maxwell Hammer wrote:
    > Hi all,
    >
    > This is related to an earlier post 'Help with thread related
    > tracebacks'...for which I have had no feedback yet :-(
    >
    > How should a thread complete i.e. how should it exit?
    > Reading the python online docs one gets the idea that simply returning is
    > OK - but I'm not sure.
    > Is it ok to do a sys.ext()? Use 'return' or just let them run out with no
    > 'return' ???
    >
    > thanks.
    Brian, Jun 16, 2005
    #2
    1. Advertising

  3. Maxwell Hammer

    Guest

    , Jun 16, 2005
    #3
  4. Maxwell Hammer

    Brian Guest

    Brian, Jun 16, 2005
    #4
  5. Maxwell Hammer

    Peter Hansen Guest

    Re: How should threads be terminated? (related to 'Help with threadrelated tracebacks')

    Maxwell Hammer wrote:
    > This is related to an earlier post 'Help with thread related
    > tracebacks'...for which I have had no feedback yet :-(


    If the question was well formulated, and it's been more than a couple of
    days, you should consider reposting. It's very unusual for a post with
    such a subject (if it was a clear question) to get _no_ feedback around
    here.

    > How should a thread complete i.e. how should it exit?


    As with any function, just return...

    > Reading the python online docs one gets the idea that simply returning is
    > OK - but I'm not sure.
    > Is it ok to do a sys.ext()? Use 'return' or just let them run out with no
    > 'return' ???


    sys.exit() merely raises a SystemExit exception. In the main thread
    this will terminate the application (assuming no non-daemon threads are
    still running), while in a non-main thread it should simply be ignored.

    If you aren't trying to exit from a function call within the thread,
    using "return" or just falling off the end of the run() method (if
    you've subclasses Thread) or the target function (if you used the
    "target=xxx" approach) is quite sufficient and acceptable.

    Note that "return" is identical to "return None" which is identical to
    just falling off the end of a function in Python. Some might consider a
    simple unadorned "return" to be the most expressive and readable.

    -Peter
    Peter Hansen, Jun 16, 2005
    #5
  6. On Thu, 16 Jun 2005 16:20:23 -0400, Peter Hansen wrote:

    > Maxwell Hammer wrote:
    >> This is related to an earlier post 'Help with thread related
    >> tracebacks'...for which I have had no feedback yet :-(

    >
    > If the question was well formulated, and it's been more than a couple of
    > days, you should consider reposting. It's very unusual for a post with
    > such a subject (if it was a clear question) to get _no_ feedback around
    > here.


    Fair enough. The question is not expressed clearly for others. Do you have
    any suggestions as to how to describe the problem clearer?

    I can think of no other way but to say I have an app that when I terminate
    it, completes ok, However the last thing that happens before the shell
    prompt returns is that there is a traceback *within* python.
    (The actual post goes into more details of course.)

    I just took a guess that it is *thread* related from the output of the
    traceback. I'm still learning python, so how could one pose the problem
    *clearer*?

    And thanks for the feedback regarding threads, by the way.
    Max
    Maxwell Hammer, Jun 17, 2005
    #6
  7. Maxwell Hammer, Jun 17, 2005
    #7
  8. Maxwell Hammer

    Peter Hansen Guest

    Re: How should threads be terminated? (related to 'Help with threadrelated tracebacks')

    Maxwell Hammer wrote:
    > On Thu, 16 Jun 2005 16:20:23 -0400, Peter Hansen wrote:
    >>If the question was well formulated, and it's been more than a couple of
    >>days, you should consider reposting. It's very unusual for a post with
    >>such a subject (if it was a clear question) to get _no_ feedback around
    >>here.

    >
    > Fair enough. The question is not expressed clearly for others. Do you have
    > any suggestions as to how to describe the problem clearer?


    I hope you didn't get the impression I was criticizing. I don't recall
    your post at all, and definitely wasn't suggesting that it was unclear,
    merely asking you to verify that it was and, if not, rewrite it upon
    reposting.

    As for suggestions to make it clearer: I can't make any without digging
    back for your previous posting. I generally don't take the time to do
    that since older messages are often gone from my server very quickly,
    and I don't like spending time digging around on Google Groups to find
    it. Sorry, it's just one of my approaches to conserving my own time,
    selfishly.

    > I can think of no other way but to say I have an app that when I terminate
    > it, completes ok, However the last thing that happens before the shell
    > prompt returns is that there is a traceback *within* python.
    > (The actual post goes into more details of course.)


    This sounds very much like the problem where, during the interpreter
    shutdown, all globals in all modules are rebound to None, but if daemon
    threads are still running they will quickly crash as a result and raise
    exceptions, usually referring to AttributeErrors where "None" doesn't
    have an attribute of a particular kind, usually the name of a method.

    If I'd seen your post, I would probably have responded with as much at
    the time. If you do a Google Groups search for some of those keywords
    and my name, you'll certainly find a half dozen other threads where
    someone else asked a similar question, even if I missed your post.

    > I just took a guess that it is *thread* related from the output of the
    > traceback. I'm still learning python, so how could one pose the problem
    > *clearer*?
    >
    > And thanks for the feedback regarding threads, by the way.


    No problem. And if this post didn't help, please do repost the whole
    original question so I can see it again, and those who read the group
    via the mailing list will get a fresh email, etc...

    -Peter
    Peter Hansen, Jun 18, 2005
    #8
    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. Replies:
    3
    Views:
    437
    Jack Klein
    Dec 20, 2004
  2. =?ISO-8859-1?Q?Mike_M=FCller?=

    tracebacks in embedded python

    =?ISO-8859-1?Q?Mike_M=FCller?=, Jul 9, 2003, in forum: Python
    Replies:
    1
    Views:
    320
    Thomas Heller
    Jul 9, 2003
  3. Maxwell Hammer

    Help with thread related tracebacks

    Maxwell Hammer, Jun 16, 2005, in forum: Python
    Replies:
    0
    Views:
    294
    Maxwell Hammer
    Jun 16, 2005
  4. Rustom Mody
    Replies:
    0
    Views:
    51
    Rustom Mody
    Jan 3, 2014
  5. Chris Angelico
    Replies:
    0
    Views:
    98
    Chris Angelico
    Jan 3, 2014
Loading...

Share This Page