blob: 37d8d500ef1d4e8c9ff4a60835ff17de063f034e [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001:mod:`atexit` --- Exit handlers
2===============================
3
4.. module:: atexit
5 :synopsis: Register and execute cleanup functions.
Skip Montanaro54662462007-12-08 15:26:16 +00006.. moduleauthor:: Skip Montanaro <skip@pobox.com>
7.. sectionauthor:: Skip Montanaro <skip@pobox.com>
Georg Brandl8ec7f652007-08-15 14:28:01 +00008
9
10.. versionadded:: 2.0
11
Éric Araujo29a0b572011-08-19 02:14:03 +020012**Source code:** :source:`Lib/atexit.py`
13
14--------------
15
Georg Brandl8ec7f652007-08-15 14:28:01 +000016The :mod:`atexit` module defines a single function to register cleanup
17functions. Functions thus registered are automatically executed upon normal
Éric Araujo43162e22011-07-29 18:04:24 +020018interpreter termination. The order in which the functions are called is not
19defined; if you have cleanup operations that depend on each other, you should
20wrap them in a function and register that one. This keeps :mod:`atexit` simple.
Georg Brandl8ec7f652007-08-15 14:28:01 +000021
22Note: the functions registered via this module are not called when the program
Georg Brandl420cca92010-11-26 07:21:01 +000023is killed by a signal not handled by Python, when a Python fatal internal error
24is detected, or when :func:`os._exit` is called.
Georg Brandl8ec7f652007-08-15 14:28:01 +000025
26.. index:: single: exitfunc (in sys)
27
28This is an alternate interface to the functionality provided by the
Éric Araujod9756be2012-02-15 17:08:34 +010029:func:`sys.exitfunc` variable.
Georg Brandl8ec7f652007-08-15 14:28:01 +000030
31Note: This module is unlikely to work correctly when used with other code that
32sets ``sys.exitfunc``. In particular, other core Python modules are free to use
33:mod:`atexit` without the programmer's knowledge. Authors who use
34``sys.exitfunc`` should convert their code to use :mod:`atexit` instead. The
35simplest way to convert code that sets ``sys.exitfunc`` is to import
36:mod:`atexit` and register the function that had been bound to ``sys.exitfunc``.
37
38
39.. function:: register(func[, *args[, **kargs]])
40
41 Register *func* as a function to be executed at termination. Any optional
42 arguments that are to be passed to *func* must be passed as arguments to
Éric Araujod9756be2012-02-15 17:08:34 +010043 :func:`register`. It is possible to register the same function and arguments
44 more than once.
Georg Brandl8ec7f652007-08-15 14:28:01 +000045
46 At normal program termination (for instance, if :func:`sys.exit` is called or
47 the main module's execution completes), all functions registered are called in
48 last in, first out order. The assumption is that lower level modules will
49 normally be imported before higher level modules and thus must be cleaned up
50 later.
51
52 If an exception is raised during execution of the exit handlers, a traceback is
53 printed (unless :exc:`SystemExit` is raised) and the exception information is
54 saved. After all exit handlers have had a chance to run the last exception to
55 be raised is re-raised.
56
57 .. versionchanged:: 2.6
Éric Araujod9756be2012-02-15 17:08:34 +010058 This function now returns *func*, which makes it possible to use it as a
59 decorator.
Georg Brandl8ec7f652007-08-15 14:28:01 +000060
61
62.. seealso::
63
64 Module :mod:`readline`
65 Useful example of :mod:`atexit` to read and write :mod:`readline` history files.
66
67
68.. _atexit-example:
69
70:mod:`atexit` Example
71---------------------
72
73The following simple example demonstrates how a module can initialize a counter
74from a file when it is imported and save the counter's updated value
75automatically when the program terminates without relying on the application
76making an explicit call into this module at termination. ::
77
78 try:
Petri Lehtinen0b785032013-02-23 19:24:08 +010079 _count = int(open("counter").read())
Georg Brandl8ec7f652007-08-15 14:28:01 +000080 except IOError:
81 _count = 0
82
83 def incrcounter(n):
84 global _count
85 _count = _count + n
86
87 def savecounter():
Petri Lehtinen0b785032013-02-23 19:24:08 +010088 open("counter", "w").write("%d" % _count)
Georg Brandl8ec7f652007-08-15 14:28:01 +000089
90 import atexit
91 atexit.register(savecounter)
92
93Positional and keyword arguments may also be passed to :func:`register` to be
94passed along to the registered function when it is called::
95
96 def goodbye(name, adjective):
97 print 'Goodbye, %s, it was %s to meet you.' % (name, adjective)
98
99 import atexit
100 atexit.register(goodbye, 'Donny', 'nice')
101
102 # or:
103 atexit.register(goodbye, adjective='nice', name='Donny')
104
Georg Brandl584265b2007-12-02 14:58:50 +0000105Usage as a :term:`decorator`::
Georg Brandl8ec7f652007-08-15 14:28:01 +0000106
107 import atexit
108
109 @atexit.register
110 def goodbye():
111 print "You are now leaving the Python sector."
112
Éric Araujod9756be2012-02-15 17:08:34 +0100113This only works with functions that can be called without arguments.