1:mod:`atexit` --- Exit handlers 2=============================== 3 4.. module:: atexit 5 :synopsis: Register and execute cleanup functions. 6 7.. moduleauthor:: Skip Montanaro <skip@pobox.com> 8.. sectionauthor:: Skip Montanaro <skip@pobox.com> 9 10-------------- 11 12The :mod:`atexit` module defines functions to register and unregister cleanup 13functions. Functions thus registered are automatically executed upon normal 14interpreter termination. :mod:`atexit` runs these functions in the *reverse* 15order in which they were registered; if you register ``A``, ``B``, and ``C``, 16at 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 20program is killed by a signal not handled by Python, when a Python fatal 21internal error is detected, or when :func:`os._exit` is called. 22 23.. versionchanged:: 3.7 24 When used with C-API subinterpreters, registered functions 25 are local to the interpreter they were registered in. 26 27.. function:: register(func, *args, **kwargs) 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 31 :func:`register`. It is possible to register the same function and arguments 32 more than once. 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 45 This function returns *func*, which makes it possible to use it as a 46 decorator. 47 48 49.. function:: unregister(func) 50 51 Remove *func* from the list of functions to be run at interpreter shutdown. 52 :func:`unregister` silently does nothing if *func* was not previously 53 registered. If *func* has been registered more than once, every occurrence 54 of that function in the :mod:`atexit` call stack will be removed. Equality 55 comparisons (``==``) are used internally during unregistration, so function 56 references do not need to have matching identities. 57 58 59.. seealso:: 60 61 Module :mod:`readline` 62 Useful example of :mod:`atexit` to read and write :mod:`readline` history 63 files. 64 65 66.. _atexit-example: 67 68:mod:`atexit` Example 69--------------------- 70 71The following simple example demonstrates how a module can initialize a counter 72from a file when it is imported and save the counter's updated value 73automatically when the program terminates without relying on the application 74making an explicit call into this module at termination. :: 75 76 try: 77 with open('counterfile') as infile: 78 _count = int(infile.read()) 79 except FileNotFoundError: 80 _count = 0 81 82 def incrcounter(n): 83 global _count 84 _count = _count + n 85 86 def savecounter(): 87 with open('counterfile', 'w') as outfile: 88 outfile.write('%d' % _count) 89 90 import atexit 91 92 atexit.register(savecounter) 93 94Positional and keyword arguments may also be passed to :func:`register` to be 95passed along to the registered function when it is called:: 96 97 def goodbye(name, adjective): 98 print('Goodbye %s, it was %s to meet you.' % (name, adjective)) 99 100 import atexit 101 102 atexit.register(goodbye, 'Donny', 'nice') 103 # or: 104 atexit.register(goodbye, adjective='nice', name='Donny') 105 106Usage as a :term:`decorator`:: 107 108 import atexit 109 110 @atexit.register 111 def goodbye(): 112 print('You are now leaving the Python sector.') 113 114This only works with functions that can be called without arguments. 115