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