Can’t register atexit after shutdown

It is not possible to register a new atexit function after the shutdown process has started. By the time the shutdown process begins,
all atexit functions should have been registered and will be called in the reverse order of their registration.

The atexit function in Python is used to register functions that are called when the interpreter is about to exit. These functions are
registered using the atexit.register() function. The purpose of these functions is to perform cleanup actions or release
resources before the program terminates.

Here is an example to illustrate registering and executing atexit functions:

import atexit

def cleanup1():
    print("Performing cleanup 1")

def cleanup2():
    print("Performing cleanup 2")

atexit.register(cleanup1)
atexit.register(cleanup2)

# Rest of the program

# Output:
# Rest of the program
# Performing cleanup 2
# Performing cleanup 1
  

In the above example, two cleanup functions, cleanup1 and cleanup2, are registered using atexit.register().
When the program finishes execution, the cleanup functions will be called in the reverse order of their registration, resulting in the
output shown above.

However, it is important to note that once the shutdown process has started, it is not possible to register additional atexit functions.
Any attempt to do so will have no effect.

Similar post

Leave a comment