472,144 Members | 1,953 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,144 software developers and data experts.

os._exit vs. sys.exit

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
4 28352
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
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

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

Similar topics

12 posts views Thread by Ivan Voras | last post: by
5 posts views Thread by Simon Faulkner | last post: by
reply views Thread by Andrew Athan | last post: by
12 posts views Thread by Siemel Naran | last post: by
20 posts views Thread by lovecreatesbeauty | last post: by
1 post views Thread by Spiros Bousbouras | last post: by
reply views Thread by Tim Golden | last post: by
reply views Thread by Saiars | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.