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

os._exit vs. sys.exit

P: n/a
Quick question:

Why does os._exit called from a Python Timer kill the whole process while
sys.exit does not? On Suse.

Bryan
Jul 29 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Bryan wrote:
Why does os._exit called from a Python Timer kill the whole process while
sys.exit does not? On Suse.


os._exit calls the C function _exit() which does an immediate program
termination. See for example
http://developer.apple.com/documenta...2/_exit.2.html
and note the statement "can never return".

sys.exit() is identical to "raise SystemExit()". It raises a Python
exception which may be caught at a higher level in the program stack.

Andrew
da***@dalkescientific.com
Jul 29 '05 #2

P: n/a
Andrew Dalke wrote:
sys.exit() is identical to "raise SystemExit()". It raises a Python
exception which may be caught at a higher level in the program stack.


And which *is* caught at the highest levels of threading.Thread objects
(which Timer is based on). Exceptions raised (and caught or not) in a
Thread do not have any effect on the main thread, and thus don't affect
the interpreter as a whole.

-Peter
Jul 29 '05 #3

P: n/a

"Peter Hansen" <pe***@engcorp.com> wrote in message
news:u-********************@powergate.ca...
Andrew Dalke wrote:
sys.exit() is identical to "raise SystemExit()". It raises a Python
exception which may be caught at a higher level in the program stack.


And which *is* caught at the highest levels of threading.Thread objects
(which Timer is based on). Exceptions raised (and caught or not) in a
Thread do not have any effect on the main thread, and thus don't affect
the interpreter as a whole.

-Peter


Thanks for the clarifications. One more question, can I catch this
exception in my main thread and then do another sys.exit() to kill the whole
process?

Apparently sys.exit() allows the program to clean up resources and exit
gracefully, while os._exit() is rather abrupt.

Bryan
Jul 29 '05 #4

P: n/a
Bryan wrote:
Thanks for the clarifications. One more question, can I catch this
exception in my main thread and then do another sys.exit() to kill the whole
process?
Not as such. Exceptions can be caught only in the thread in which they
are raised. There are tricky techniques to change this, but they would
have to rely on things which are themselves sufficient for what you are
trying to do.
Apparently sys.exit() allows the program to clean up resources and exit
gracefully, while os._exit() is rather abrupt.


What does the main thread do while the other thread is running? If it's
just waiting for it and other threads to finish/fail, then you need to
have some kind of loop that waits for all other threads to not respond
True to .isAlive(), and/or you need to add a threading.Event or
something like it which the main thread can wait on or poll to see
whether a thread has caught an exception, *and* you need to make all
those other threads catch their own exceptions at the top levels of
their run() method, and to set that Event object if SystemExit is
caught. Or related techniques.

If that's not enough ideas for you to figure something out, please
provide more detail and we can come up with something more specific and
appropriate. For example, do you want to exit the app only if a thread
raises SystemExit, or would other exceptions result in the same effect?

-Peter
Jul 29 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.