Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | :mod:`atexit` --- Exit handlers |
| 2 | =============================== |
| 3 | |
| 4 | .. module:: atexit |
| 5 | :synopsis: Register and execute cleanup functions. |
Terry Jan Reedy | fa089b9 | 2016-06-11 15:02:54 -0400 | [diff] [blame] | 6 | |
Christian Heimes | 895627f | 2007-12-08 17:28:33 +0000 | [diff] [blame] | 7 | .. moduleauthor:: Skip Montanaro <skip@pobox.com> |
| 8 | .. sectionauthor:: Skip Montanaro <skip@pobox.com> |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 9 | |
Terry Jan Reedy | fa089b9 | 2016-06-11 15:02:54 -0400 | [diff] [blame] | 10 | -------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 11 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 12 | The :mod:`atexit` module defines functions to register and unregister cleanup |
| 13 | functions. Functions thus registered are automatically executed upon normal |
Larry Hastings | 1191709 | 2012-07-14 18:20:37 -0700 | [diff] [blame] | 14 | interpreter termination. :mod:`atexit` runs these functions in the *reverse* |
| 15 | order in which they were registered; if you register ``A``, ``B``, and ``C``, |
| 16 | at interpreter termination time they will be run in the order ``C``, ``B``, |
| 17 | ``A``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 18 | |
Larry Hastings | 1191709 | 2012-07-14 18:20:37 -0700 | [diff] [blame] | 19 | **Note:** The functions registered via this module are not called when the |
| 20 | program is killed by a signal not handled by Python, when a Python fatal |
| 21 | internal error is detected, or when :func:`os._exit` is called. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 22 | |
| 23 | |
Georg Brandl | b868a66 | 2009-04-02 02:56:10 +0000 | [diff] [blame] | 24 | .. function:: register(func, *args, **kargs) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 25 | |
| 26 | Register *func* as a function to be executed at termination. Any optional |
| 27 | arguments that are to be passed to *func* must be passed as arguments to |
Éric Araujo | ccddc47 | 2012-02-15 17:07:49 +0100 | [diff] [blame] | 28 | :func:`register`. It is possible to register the same function and arguments |
| 29 | more than once. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 30 | |
| 31 | At normal program termination (for instance, if :func:`sys.exit` is called or |
| 32 | the main module's execution completes), all functions registered are called in |
| 33 | last in, first out order. The assumption is that lower level modules will |
| 34 | normally be imported before higher level modules and thus must be cleaned up |
| 35 | later. |
| 36 | |
| 37 | If an exception is raised during execution of the exit handlers, a traceback is |
| 38 | printed (unless :exc:`SystemExit` is raised) and the exception information is |
| 39 | saved. After all exit handlers have had a chance to run the last exception to |
| 40 | be raised is re-raised. |
| 41 | |
Éric Araujo | ccddc47 | 2012-02-15 17:07:49 +0100 | [diff] [blame] | 42 | This function returns *func*, which makes it possible to use it as a |
| 43 | decorator. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 44 | |
| 45 | |
| 46 | .. function:: unregister(func) |
| 47 | |
Éric Araujo | ccddc47 | 2012-02-15 17:07:49 +0100 | [diff] [blame] | 48 | Remove *func* from the list of functions to be run at interpreter |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 49 | shutdown. After calling :func:`unregister`, *func* is guaranteed not to be |
Éric Araujo | ccddc47 | 2012-02-15 17:07:49 +0100 | [diff] [blame] | 50 | called when the interpreter shuts down, even if it was registered more than |
| 51 | once. :func:`unregister` silently does nothing if *func* was not previously |
| 52 | registered. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 53 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 54 | |
| 55 | .. seealso:: |
| 56 | |
| 57 | Module :mod:`readline` |
Georg Brandl | b868a66 | 2009-04-02 02:56:10 +0000 | [diff] [blame] | 58 | Useful example of :mod:`atexit` to read and write :mod:`readline` history |
| 59 | files. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 60 | |
| 61 | |
| 62 | .. _atexit-example: |
| 63 | |
| 64 | :mod:`atexit` Example |
| 65 | --------------------- |
| 66 | |
| 67 | The following simple example demonstrates how a module can initialize a counter |
| 68 | from a file when it is imported and save the counter's updated value |
| 69 | automatically when the program terminates without relying on the application |
| 70 | making an explicit call into this module at termination. :: |
| 71 | |
| 72 | try: |
Petri Lehtinen | 3c75a48 | 2013-02-23 19:34:15 +0100 | [diff] [blame] | 73 | with open("counterfile") as infile: |
Éric Araujo | e4f6a80 | 2011-03-12 15:56:09 +0100 | [diff] [blame] | 74 | _count = int(infile.read()) |
Antoine Pitrou | 62ab10a0 | 2011-10-12 20:10:51 +0200 | [diff] [blame] | 75 | except FileNotFoundError: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 76 | _count = 0 |
| 77 | |
| 78 | def incrcounter(n): |
| 79 | global _count |
| 80 | _count = _count + n |
| 81 | |
| 82 | def savecounter(): |
Petri Lehtinen | 3c75a48 | 2013-02-23 19:34:15 +0100 | [diff] [blame] | 83 | with open("counterfile", "w") as outfile: |
Éric Araujo | a3dd56b | 2011-03-11 17:42:48 +0100 | [diff] [blame] | 84 | outfile.write("%d" % _count) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 85 | |
| 86 | import atexit |
| 87 | atexit.register(savecounter) |
| 88 | |
| 89 | Positional and keyword arguments may also be passed to :func:`register` to be |
| 90 | passed along to the registered function when it is called:: |
| 91 | |
| 92 | def goodbye(name, adjective): |
Georg Brandl | 6911e3c | 2007-09-04 07:15:32 +0000 | [diff] [blame] | 93 | print('Goodbye, %s, it was %s to meet you.' % (name, adjective)) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 94 | |
| 95 | import atexit |
| 96 | atexit.register(goodbye, 'Donny', 'nice') |
| 97 | |
| 98 | # or: |
| 99 | atexit.register(goodbye, adjective='nice', name='Donny') |
| 100 | |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 101 | Usage as a :term:`decorator`:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 102 | |
| 103 | import atexit |
| 104 | |
| 105 | @atexit.register |
| 106 | def goodbye(): |
Georg Brandl | 6911e3c | 2007-09-04 07:15:32 +0000 | [diff] [blame] | 107 | print("You are now leaving the Python sector.") |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 108 | |
Éric Araujo | ccddc47 | 2012-02-15 17:07:49 +0100 | [diff] [blame] | 109 | This only works with functions that can be called without arguments. |