473,466 Members | 1,391 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 28965
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Ivan Voras | last post by:
In a code such as: if len(sys.argv) < 2: print "I need arguments!" sys.exit(1) Is sys.exit() really a good choice? Is there something more elegant? (I tried return but it is valid only in a...
5
by: Simon Faulkner | last post by:
Does Python have a command that just stops all processing? Simon
0
by: Andrew Athan | last post by:
I have a python program (snippet below) which does not want to seem to die when I issue a sys.exit() inside the SIGTERM handler. The output below is generated as the result of sending several...
12
by: Siemel Naran | last post by:
(1) About std::exit. If I call this function, will the system call the destructors of all objects in the call stack. Eg. void f() { std::exit(1); } void g() { X x; f(); } Does the call in...
5
by: QQ | last post by:
I know there are many functions that I can exit the program such as return 0, exit(0), exit(1),_EXIT(0) .... What are the difference between them? Thanks a lot!
20
by: lovecreatesbeauty | last post by:
Hello experts, Is the following code snippet legal? If it is, how can exit() do the keyword return a favor and give a return value to the main function? Can a function call (or only this...
2
by: devsudhir | last post by:
Hi All, I have a python program which was developed to for USB commincation. This program writes its results to .csv file.All is working fine on the PC with which we developed this program. Now I...
1
by: Spiros Bousbouras | last post by:
In 7.20.4.4 of n1124 we read: The _Exit function causes normal program termination to occur and control to be returned to the host environment. No functions registered by the atexit function...
0
by: Tim Golden | last post by:
Robert Rawlins wrote: You want to look at the atexit module: http://docs.python.org/lib/module-atexit.html but mind the caveats: """ Note: the functions registered via this module are not
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.