os._exit vs. sys.exit
peter at engcorp.com
Fri Jul 29 14:21:24 CEST 2005
> 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
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?
More information about the Python-list