On this page
atexit.register
Up: atexit | Next: atexit.unregister
Rel: atexit.unregister
Ref: docs.python.org — atexit.register
Tags: #public #python3 #stdlib #atexit
Import
import atexit
from atexit import register
Signature & Overview
atexit.register(func, /, *args, **kwargs)
Register a function to be executed upon normal program termination func - function to be called at exit args - optional arguments to pass to func
Usage
import atexit
cleanup_log: list[str] = []
fn = lambda: cleanup_log.append("done")
atexit.register(fn)
atexit.unregister(fn)
Gotchas
- LIFO execution order on normal interpreter exit: Does not run on
os._exit(),SIGKILL, or C crashes. - Don't rely on
atexitfor file/lock cleanup: Usewithcontext managers wherever possible.
REPL Inspect
from atexit import register
help(register)
Linked from
Related notes
- tuts/python3/stdlib_module/atexit/unregister Linked here; links to this page; shared tags: #atexit, #stdlib
- tuts/python3/stdlib_module/atexit Linked here; links to this page; shared tags: #stdlib
- tuts/c/stdlib shared tags: #stdlib
- tuts/c/stdlib/assert shared tags: #stdlib
- tuts/c/stdlib/assert/assert shared tags: #stdlib
- tuts/c/stdlib/assert/static_assert shared tags: #stdlib
- tuts/c/stdlib/ctype shared tags: #stdlib
- tuts/c/stdlib/ctype/isalpha shared tags: #stdlib
- tuts/c/stdlib/ctype/isdigit shared tags: #stdlib
- tuts/c/stdlib/ctype/isspace shared tags: #stdlib