blob: 5b87b94ca153258d3f2d41251960406f6e2282da [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`atexit` --- Exit handlers
2===============================
3
4.. module:: atexit
5 :synopsis: Register and execute cleanup functions.
Christian Heimes895627f2007-12-08 17:28:33 +00006.. moduleauthor:: Skip Montanaro <skip@pobox.com>
7.. sectionauthor:: Skip Montanaro <skip@pobox.com>
Georg Brandl116aa622007-08-15 14:28:22 +00008
9
Georg Brandl116aa622007-08-15 14:28:22 +000010The :mod:`atexit` module defines functions to register and unregister cleanup
11functions. Functions thus registered are automatically executed upon normal
Éric Araujofe1e2982011-07-29 18:04:24 +020012interpreter termination. The order in which the functions are called is not
13defined; if you have cleanup operations that depend on each other, you should
14wrap them in a function and register that one. This keeps :mod:`atexit` simple.
Georg Brandl116aa622007-08-15 14:28:22 +000015
16Note: the functions registered via this module are not called when the program
Georg Brandl7c4cad52010-10-14 06:43:22 +000017is killed by a signal not handled by Python, when a Python fatal internal error
18is detected, or when :func:`os._exit` is called.
Georg Brandl116aa622007-08-15 14:28:22 +000019
20
Georg Brandlb868a662009-04-02 02:56:10 +000021.. function:: register(func, *args, **kargs)
Georg Brandl116aa622007-08-15 14:28:22 +000022
23 Register *func* as a function to be executed at termination. Any optional
24 arguments that are to be passed to *func* must be passed as arguments to
25 :func:`register`.
26
27 At normal program termination (for instance, if :func:`sys.exit` is called or
28 the main module's execution completes), all functions registered are called in
29 last in, first out order. The assumption is that lower level modules will
30 normally be imported before higher level modules and thus must be cleaned up
31 later.
32
33 If an exception is raised during execution of the exit handlers, a traceback is
34 printed (unless :exc:`SystemExit` is raised) and the exception information is
35 saved. After all exit handlers have had a chance to run the last exception to
36 be raised is re-raised.
37
Georg Brandl55ac8f02007-09-01 13:51:09 +000038 This function returns *func* which makes it possible to use it as a decorator
39 without binding the original name to ``None``.
Georg Brandl116aa622007-08-15 14:28:22 +000040
41
42.. function:: unregister(func)
43
44 Remove a function *func* from the list of functions to be run at interpreter-
45 shutdown. After calling :func:`unregister`, *func* is guaranteed not to be
46 called when the interpreter shuts down.
47
Georg Brandl116aa622007-08-15 14:28:22 +000048
49.. seealso::
50
51 Module :mod:`readline`
Georg Brandlb868a662009-04-02 02:56:10 +000052 Useful example of :mod:`atexit` to read and write :mod:`readline` history
53 files.
Georg Brandl116aa622007-08-15 14:28:22 +000054
55
56.. _atexit-example:
57
58:mod:`atexit` Example
59---------------------
60
61The following simple example demonstrates how a module can initialize a counter
62from a file when it is imported and save the counter's updated value
63automatically when the program terminates without relying on the application
64making an explicit call into this module at termination. ::
65
66 try:
67 _count = int(open("/tmp/counter").read())
68 except IOError:
69 _count = 0
70
71 def incrcounter(n):
72 global _count
73 _count = _count + n
74
75 def savecounter():
76 open("/tmp/counter", "w").write("%d" % _count)
77
78 import atexit
79 atexit.register(savecounter)
80
81Positional and keyword arguments may also be passed to :func:`register` to be
82passed along to the registered function when it is called::
83
84 def goodbye(name, adjective):
Georg Brandl6911e3c2007-09-04 07:15:32 +000085 print('Goodbye, %s, it was %s to meet you.' % (name, adjective))
Georg Brandl116aa622007-08-15 14:28:22 +000086
87 import atexit
88 atexit.register(goodbye, 'Donny', 'nice')
89
90 # or:
91 atexit.register(goodbye, adjective='nice', name='Donny')
92
Christian Heimesd8654cf2007-12-02 15:22:16 +000093Usage as a :term:`decorator`::
Georg Brandl116aa622007-08-15 14:28:22 +000094
95 import atexit
96
97 @atexit.register
98 def goodbye():
Georg Brandl6911e3c2007-09-04 07:15:32 +000099 print("You are now leaving the Python sector.")
Georg Brandl116aa622007-08-15 14:28:22 +0000100
101This obviously only works with functions that don't take arguments.
102
Antoine Pitrou1bdd6fd2011-01-07 18:42:21 +0000103