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

M

Maxwell Hammer

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.
 
B

Brian

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 :)
 
P

Peter Hansen

Maxwell said:
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
 
M

Maxwell Hammer

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
 
P

Peter Hansen

Maxwell said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top