94 lines · 3.2 KB · TeX
RawDownload
\section{\module{atexit} ---         Exit handlers} \declaremodule{standard}{atexit}\moduleauthor{Skip Montanaro}{skip@mojam.com}\sectionauthor{Skip Montanaro}{skip@mojam.com}\modulesynopsis{Register and execute cleanup functions.} \versionadded{2.0} The \module{atexit} module defines a single function to registercleanup functions.  Functions thus registered are automaticallyexecuted upon normal interpreter termination. Note: the functions registered via this module are not called when the program is killed by asignal, when a Python fatal internal error is detected, or when\function{os._exit()} is called. This is an alternate interface to the functionality provided by the\code{sys.exitfunc} variable.\withsubitem{(in sys)}{\ttindex{exitfunc}} Note: This module is unlikely to work correctly when used with other codethat sets \code{sys.exitfunc}.  In particular, other core Python modules arefree to use \module{atexit} without the programmer's knowledge.  Authors whouse \code{sys.exitfunc} should convert their code to use\module{atexit} instead.  The simplest way to convert code that sets\code{sys.exitfunc} is to import \module{atexit} and register the functionthat had been bound to \code{sys.exitfunc}. \begin{funcdesc}{register}{func\optional{, *args\optional{, **kargs}}}Register \var{func} as a function to be executed at termination.  Anyoptional arguments that are to be passed to \var{func} must be passedas arguments to \function{register()}. At normal program termination (for instance, if\function{sys.exit()} is called or the main module's executioncompletes), all functions registered are called in last in, first outorder.  The assumption is that lower level modules will normally beimported before higher level modules and thus must be cleaned uplater. If an exception is raised during execution of the exit handlers, atraceback is printed (unless \exception{SystemExit} is raised) and theexception information is saved.  After all exit handlers have had achance to run the last exception to be raised is re-raised.\end{funcdesc}  \begin{seealso}  \seemodule{readline}{Useful example of \module{atexit} to read and                       write \refmodule{readline} history files.}\end{seealso}  \subsection{\module{atexit} Example \label{atexit-example}} The following simple example demonstrates how a module can initializea counter from a file when it is imported and save the counter'supdated value automatically when the program terminates withoutrelying on the application making an explicit call into this module attermination. \begin{verbatim}try:    _count = int(open("/tmp/counter").read())except IOError:    _count = 0 def incrcounter(n):    global _count    _count = _count + n def savecounter():    open("/tmp/counter", "w").write("%d" % _count) import atexitatexit.register(savecounter)\end{verbatim} Positional and keyword arguments may also be passed to\function{register()} to be passed along to the registered functionwhen it is called: \begin{verbatim}def goodbye(name, adjective):    print 'Goodbye, %s, it was %s to meet you.' % (name, adjective) import atexitatexit.register(goodbye, 'Donny', 'nice') # or:atexit.register(goodbye, adjective='nice', name='Donny')\end{verbatim}