os._exit vs. sys.exit

B

Bryan

Quick question:

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

Bryan
 
A

Andrew Dalke

Bryan said:
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/documentation/Darwin/Reference/ManPages/man2/_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
(e-mail address removed)
 
P

Peter Hansen

Andrew said:
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
 
B

Bryan

Peter Hansen said:
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
 
P

Peter Hansen

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

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top