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