For a simple python script that doesn't start any threads or fork any processes, what's the difference between simply letting execution reach the end of the script and explicitly calling quit(), exit(), or sys.exit()?
i.e. what's the difference between
def main():
# do some stuff
print("Done doing stuff")
if __name__ == '__main__':
main()
and
import sys
def main():
# do some stuff
print("Done doing stuff")
# explicit exit
sys.exit()
if __name__ == '__main__':
main()
I'm asking this because I have been having some odd garbage collection issues on script termination (exceptions raised from __del__
apparently due to the order in which things get cleaned up) without the explicit exit()
call, and adding an explicit exit()
appears to correct those issues. Or at least none of those exceptions get printed after calling exit()
, maybe they are simply being silenced.
sys.exit()
allows the 'finally' clause, wherein the clean-up functionality is built, of some try blocks to activate by raising an exception, thereby giving a "cleaner" exit. – Uvarsys.exit()
? – alex.forencich