1 :mod:`atexit` --- Exit handlers 2 =============================== 3 4 .. module:: atexit 5 :synopsis: Register and execute cleanup functions. 6 7 .. moduleauthor:: Skip Montanaro <skip (a] pobox.com> 8 .. sectionauthor:: Skip Montanaro <skip (a] pobox.com> 9 10 -------------- 11 12 The :mod:`atexit` module defines functions to register and unregister cleanup 13 functions. Functions thus registered are automatically executed upon normal 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``. 18 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. 22 23 24 .. function:: register(func, *args, **kargs) 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`. It is possible to register the same function and arguments 29 more than once. 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 42 This function returns *func*, which makes it possible to use it as a 43 decorator. 44 45 46 .. function:: unregister(func) 47 48 Remove *func* from the list of functions to be run at interpreter 49 shutdown. After calling :func:`unregister`, *func* is guaranteed not to be 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. 53 54 55 .. seealso:: 56 57 Module :mod:`readline` 58 Useful example of :mod:`atexit` to read and write :mod:`readline` history 59 files. 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: 73 with open("counterfile") as infile: 74 _count = int(infile.read()) 75 except FileNotFoundError: 76 _count = 0 77 78 def incrcounter(n): 79 global _count 80 _count = _count + n 81 82 def savecounter(): 83 with open("counterfile", "w") as outfile: 84 outfile.write("%d" % _count) 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): 93 print('Goodbye, %s, it was %s to meet you.' % (name, adjective)) 94 95 import atexit 96 atexit.register(goodbye, 'Donny', 'nice') 97 98 # or: 99 atexit.register(goodbye, adjective='nice', name='Donny') 100 101 Usage as a :term:`decorator`:: 102 103 import atexit 104 105 @atexit.register 106 def goodbye(): 107 print("You are now leaving the Python sector.") 108 109 This only works with functions that can be called without arguments. 110