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