First candidate is os._exit
, which exits without calling cleanup handlers, flushing stdio buffers, etc. (and which should normally only be used in the child process after a fork()
).
Second, sys.exit
is intended for use in programs. According to official document:
This is implemented by raising the
SystemExit
exception, so cleanup actions specified by finally clauses of try statements are honored, and it is possible to intercept the exit attempt at an outer level. Sinceexit()
ultimately “only” raises an exception, it will only exit the process when called from the main thread, and the exception is not intercepted.
Third, exit
is a helper for the interactive shell and will also throw SystemExit
exception.
Reference: