By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,621 Members | 1,077 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,621 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.
Jul 19 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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.

Jul 19 '05 #2

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

Jul 19 '05 #3

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

http://www.codesampler.com/python.htm
(Look for the "Spawning Threads" section.)

Brian
---
wi******@hotmail.com wrote:
see http://aspn.activestate.com/ASPN/Coo...n/Recipe/65448 for a
useful recipe on how to do threading

Jul 19 '05 #4

P: n/a
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
Jul 19 '05 #5

P: n/a
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

Jul 19 '05 #6

P: n/a
Thanks Brian & Martin for the links.
I actually found another good one:
http://linuxgazette.net/107/pai.html

Cheers.
Jul 19 '05 #7

P: n/a
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
Jul 19 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.