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 |
| 12 | interpreter termination. |
| 13 | |
Éric Araujo | 6e6cb8e | 2010-11-16 19:13:50 +0000 | [diff] [blame] | 14 | .. seealso:: |
| 15 | |
| 16 | Latest version of the :source:`atexit Python source code |
| 17 | <Lib/atexit.py>` |
| 18 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 19 | 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] | 20 | is killed by a signal not handled by Python, when a Python fatal internal error |
| 21 | 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 |
| 28 | :func:`register`. |
| 29 | |
| 30 | At normal program termination (for instance, if :func:`sys.exit` is called or |
| 31 | the main module's execution completes), all functions registered are called in |
| 32 | last in, first out order. The assumption is that lower level modules will |
| 33 | normally be imported before higher level modules and thus must be cleaned up |
| 34 | later. |
| 35 | |
| 36 | If an exception is raised during execution of the exit handlers, a traceback is |
| 37 | printed (unless :exc:`SystemExit` is raised) and the exception information is |
| 38 | saved. After all exit handlers have had a chance to run the last exception to |
| 39 | be raised is re-raised. |
| 40 | |
Georg Brandl | 55ac8f0 | 2007-09-01 13:51:09 +0000 | [diff] [blame] | 41 | This function returns *func* which makes it possible to use it as a decorator |
| 42 | without binding the original name to ``None``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 43 | |
| 44 | |
| 45 | .. function:: unregister(func) |
| 46 | |
| 47 | Remove a function *func* from the list of functions to be run at interpreter- |
| 48 | shutdown. After calling :func:`unregister`, *func* is guaranteed not to be |
| 49 | called when the interpreter shuts down. |
| 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: |
| 70 | _count = int(open("/tmp/counter").read()) |
| 71 | except IOError: |
| 72 | _count = 0 |
| 73 | |
| 74 | def incrcounter(n): |
| 75 | global _count |
| 76 | _count = _count + n |
| 77 | |
| 78 | def savecounter(): |
| 79 | open("/tmp/counter", "w").write("%d" % _count) |
| 80 | |
| 81 | import atexit |
| 82 | atexit.register(savecounter) |
| 83 | |
| 84 | Positional and keyword arguments may also be passed to :func:`register` to be |
| 85 | passed along to the registered function when it is called:: |
| 86 | |
| 87 | def goodbye(name, adjective): |
Georg Brandl | 6911e3c | 2007-09-04 07:15:32 +0000 | [diff] [blame] | 88 | print('Goodbye, %s, it was %s to meet you.' % (name, adjective)) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 89 | |
| 90 | import atexit |
| 91 | atexit.register(goodbye, 'Donny', 'nice') |
| 92 | |
| 93 | # or: |
| 94 | atexit.register(goodbye, adjective='nice', name='Donny') |
| 95 | |
Christian Heimes | d8654cf | 2007-12-02 15:22:16 +0000 | [diff] [blame] | 96 | Usage as a :term:`decorator`:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 97 | |
| 98 | import atexit |
| 99 | |
| 100 | @atexit.register |
| 101 | def goodbye(): |
Georg Brandl | 6911e3c | 2007-09-04 07:15:32 +0000 | [diff] [blame] | 102 | print("You are now leaving the Python sector.") |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 103 | |
| 104 | This obviously only works with functions that don't take arguments. |
| 105 | |