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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Maxwell Hammer

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

    Hi all,

    This is related to an earlier post 'Help with thread related
    tracebacks'...f or 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

    #2
    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:[color=blue]
    > Hi all,
    >
    > This is related to an earlier post 'Help with thread related
    > tracebacks'...f or 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.[/color]

    Comment

    • wittempj@hotmail.com

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

      see http://aspn.activestate.com/ASPN/Coo...n/Recipe/65448 for a
      useful recipe on how to do threading

      Comment

      • Brian

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

        Here's a site that provides an easy, *beginners* example of how to do
        threading. You might find this useful too... :-)


        (Look for the "Spawning Threads" section.)

        Brian
        ---


        wittempj@hotmai l.com wrote:[color=blue]
        > see http://aspn.activestate.com/ASPN/Coo...n/Recipe/65448 for a
        > useful recipe on how to do threading
        >[/color]

        Comment

        • Peter Hansen

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

          Maxwell Hammer wrote:[color=blue]
          > This is related to an earlier post 'Help with thread related
          > tracebacks'...f or which I have had no feedback yet :-([/color]

          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.
          [color=blue]
          > How should a thread complete i.e. how should it exit?[/color]

          As with any function, just return...
          [color=blue]
          > 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' ???[/color]

          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

          Comment

          • Maxwell Hammer

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

            On Thu, 16 Jun 2005 16:20:23 -0400, Peter Hansen wrote:
            [color=blue]
            > Maxwell Hammer wrote:[color=green]
            >> This is related to an earlier post 'Help with thread related
            >> tracebacks'...f or which I have had no feedback yet :-([/color]
            >
            > 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.[/color]

            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

            Comment

            • Maxwell Hammer

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

              Thanks Brian & Martin for the links.
              I actually found another good one:


              Cheers.

              Comment

              • Peter Hansen

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

                Maxwell Hammer wrote:[color=blue]
                > On Thu, 16 Jun 2005 16:20:23 -0400, Peter Hansen wrote:[color=green]
                >>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.[/color]
                >
                > Fair enough. The question is not expressed clearly for others. Do you have
                > any suggestions as to how to describe the problem clearer?[/color]

                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.
                [color=blue]
                > 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.)[/color]

                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.
                [color=blue]
                > 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.[/color]

                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

                Comment

                Working...