03 September 2022

In Python, such as many others pragramming language, it is possibile to implement an exit gracefully from a program.
Note: I created a repository with a full example --> https://gitlab.com/ipsedixit-org/python-exit-gracefully-example

There two concept explained in this post:

System exit

In hava there is a special instruction used to force a program to end immediately and it is:

exit(exit_value);

Where:

Calling exit interrupt immediately the program and the are no more instruction execute.
So please pay attention to use exit carefully because could create a memory leak on opened resources.

Catch interrupt signal

It is possible to register a shutdown hook using signal.signal(signal.SIGINT, handler), where:

References