8 – Schedule cleanup actions#
If you need to clean up resources when your Python program terminates, (for example, disconnect from a server or database), you can use the function register from the module atexit.
You pass in a function to register, and the function you pass it is scheduled to run when your program terminates (even if it terminates because of an exception).
register can also be used as a decorator:
import atexit
@atexit.register
def cleanup():
"""Clean up program resources."""
fake_db.close_connection()
print("All cleaned up!")