AtExit (atexit) Module
The atexit module in Python allows you to register functions that will run automatically when the program is about to exit.
- This is useful for performing cleanup tasks like closing files, releasing resources, or printing goodbye messages.
Basic Usage of atexit
-
Registering Functions: Use
atexit.register(function_name)to register a function that will be called upon program termination. -
Automatic Execution: Registered functions are executed in the reverse order of their registration when the program exits normally (no crashes or
os._exit). -
Unregistering Functions: You can unregister a function using
atexit.unregister(function_name).
Registering a Function
import atexit
def goodbye():
print("Program is exiting... Goodbye!")
# Register the function
atexit.register(goodbye)
# Program logic
print("Hello, World!")
Output:
Multiple Registered Functions
import atexit
def func1():
print("First cleanup function.")
def func2():
print("Second cleanup function.")
# Register both functions
atexit.register(func1)
atexit.register(func2)
print("Program is running...")
Output:
Note: Functions are called in reverse order of their registration (func2 runs before func1).
Passing Arguments to Functions
You can use atexit.register with additional arguments:
import atexit
def greet(name):
print(f"Goodbye, {name}!")
# Register with arguments
atexit.register(greet, "Alice")
print("Program is running...")
Output:
Using lambda Functions
You can register anonymous functions with lambda:
import atexit
atexit.register(lambda: print("Lambda function executed!"))
print("Program is running...")
Output:
atexit decorator
Key Points to Remember
-
Normal Exit Only: Functions registered with
atexitare executed only during a normal exit (not if the program crashes oros._exit()is called). -
Reverse Execution: Functions are executed in reverse order of their registration.
-
Use Cases: Cleanup tasks, logging program termination, closing open resources.
Summary
- The
atexitmodule is a handy way to manage cleanup tasks when your program exits. - By registering functions with
atexit.register, you ensure they run automatically at termination. - Itβs particularly useful for tasks like saving state, closing connections, or releasing resources.