Return vs sys.exit()

Consider a program with threads and lots of asynchronous stuff. I have a main where at the end of it somebody has written “sys.exit(0)”. And in catching exceptions at some places there’s sys.exit(1). But I want to return some data at the end of main. If I use return statement above sys.exit(0), I can return data but the sys.exit(0) is basically useless right? Coz it will never go there even if there’s error in my data “thing”.
So I want to confirm if writing “return” at the end of main is same as writing sys.exit (0 or 1 both). This “return” will clear the memory immediately just as sys.exit does right?

Thanks

From where is your main function being called? That is what really matters. In general, no, wirting sys.exit() and return are not the same thing.

But yes, putting anything in a function after a return statement is completely useless and that code will never be executed.

1 Like

@MegaIng at the end of this python script main() is called if __ name __ is main, which is indirectly a shell script that calls it. And I want to return data in that shell script.
My doubt is sys.exit at the end of main or end of everything is same as return right?

To where do you want to return what kind of data to? Can you provide a more complete example?

I’ve got a number of script-ish utilities with a flow along these
lines:

def main():
    ...
    return exitcode

if __name__ == '__main__':
    sys.exit(main())

This allows command-line callers to receive the exit code in the
usual ways, but library-esque callers can handle the exit code
independently by calling the main() function instead.

[Edit: Fixed my mailercode example after actually looking at
one of my scripts.]

You can experiment with how python sets the exit code with an example like this:

import sys

def main():
    if 'raise' in sys.argv:
        raise ValueError('Bang')
    if 'number' in sys.argv:
        return 7
    return

if __name__ == '__main__':
    sys.exit(main())
% python3 a.py; echo $?
0

% python3 a.py number; echo $?
7

:  17:22:32   witcher  ~/tmpdir
: [1] barry % python3 a.py raise; echo $?
Traceback (most recent call last):
  File "/Users/barry/tmpdir/a.py", line 11, in <module>
    sys.exit(main())
             ^^^^^^
  File "/Users/barry/tmpdir/a.py", line 5, in main
    raise ValueError('Bang')
ValueError: Bang
1

Notice that returning None results in sys.exit returning 0.

It’s not clear where you are returning data, but sys.exit(0) is not useless, it signifies successful termination.

What do you mean exactly? Where should the data be “returned” to? How do you expect to use it?

If your answer is “it should be used by the shell script; the Python program should end, and then the shell script should resume by using that value” , then the answer to your question is that sys.exit is the way to do this. It is not the same as return, but it is the way that the operating system gets a value from the program (not actually your script, but Python itself).

If your answer is “it should be used by another function in the Python code; the Python program should keep running, using the value that came back from the main function”, then you need to restructure the program. Right now, main is designed to communicate back directly to the shell - it’s written with the expectation that the program finishes any time that main finishes. That’s why it was called from an if __name__ == '__main__': block: so that main will be called when the code is used as a script (but not when it’s imported as a module).

You explained that you mean

When a script ends it just ends. It’s done. It has ceased to be. It is bereft of live. It has bit the dust. The python process returns an exit code to the shell, and that’s it.
This is generally not called “returning data”. The sys.exit call is just a shortcut to exit asap from the process, with a particular exit status.

If by “returning data” you mean: actually communicate with other processes, then this is possible (you could write to a file or to the stdout, and used pipes or other mechanisms) but only while the process is still running.

1 Like

Since it seems like you’re getting a variety of not directly helpful
answers:

sys.exit() really just gets a small int out as the “return code” of
the whole programme. Handy for success (0) or failure (nonzero) but
you don’t get to send detailed data this way.

If you want to emit detailed data, the correct way is to write it out,
either to the programme output or into a file. The shell script has to
read that output or the file to make use of the data.