📄 module-atexit.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>3.3 atexit -- Exit handlers</title>
<META NAME="description" CONTENT="3.3 atexit -- Exit handlers">
<META NAME="keywords" CONTENT="lib">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="STYLESHEET" href="lib.css" tppabs="http://www.python.org/doc/current/lib/lib.css">
<LINK REL="next" href="module-types.html" tppabs="http://www.python.org/doc/current/lib/module-types.html">
<LINK REL="previous" href="module-gc.html" tppabs="http://www.python.org/doc/current/lib/module-gc.html">
<LINK REL="up" href="python.html" tppabs="http://www.python.org/doc/current/lib/python.html">
<LINK REL="next" href="atexit-example.html" tppabs="http://www.python.org/doc/current/lib/atexit-example.html">
</head>
<body>
<DIV CLASS="navigation"><table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A href="module-gc.html" tppabs="http://www.python.org/doc/current/lib/module-gc.html"><img src="previous.gif" tppabs="http://www.python.org/doc/current/icons/previous.gif" border="0" height="32"
alt="Previous Page" width="32"></A></td>
<td><A href="python.html" tppabs="http://www.python.org/doc/current/lib/python.html"><img src="up.gif" tppabs="http://www.python.org/doc/current/icons/up.gif" border="0" height="32"
alt="Up One Level" width="32"></A></td>
<td><A href="atexit-example.html" tppabs="http://www.python.org/doc/current/lib/atexit-example.html"><img src="next.gif" tppabs="http://www.python.org/doc/current/icons/next.gif" border="0" height="32"
alt="Next Page" width="32"></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td><A href="contents.html" tppabs="http://www.python.org/doc/current/lib/contents.html"><img src="contents.gif" tppabs="http://www.python.org/doc/current/icons/contents.gif" border="0" height="32"
alt="Contents" width="32"></A></td>
<td><a href="modindex.html" tppabs="http://www.python.org/doc/current/lib/modindex.html" title="Module Index"><img src="modules.gif" tppabs="http://www.python.org/doc/current/icons/modules.gif" border="0" height="32"
alt="Module Index" width="32"></a></td>
<td><A href="genindex.html" tppabs="http://www.python.org/doc/current/lib/genindex.html"><img src="index.gif" tppabs="http://www.python.org/doc/current/icons/index.gif" border="0" height="32"
alt="Index" width="32"></A></td>
</tr></table>
<b class="navlabel">Previous:</b> <a class="sectref" href="module-gc.html" tppabs="http://www.python.org/doc/current/lib/module-gc.html">3.2 gc </A>
<b class="navlabel">Up:</b> <a class="sectref" href="python.html" tppabs="http://www.python.org/doc/current/lib/python.html">3. Python Runtime Services</A>
<b class="navlabel">Next:</b> <a class="sectref" href="atexit-example.html" tppabs="http://www.python.org/doc/current/lib/atexit-example.html">3.3.1 atexit Example</A>
<br><hr></DIV>
<!--End of Navigation Panel-->
<H1><A NAME="SECTION005300000000000000000">
3.3 <tt class="module">atexit</tt> --
Exit handlers</A>
</H1>
<P>
<P>
New in version 2.0.
<P>
The <tt class="module">atexit</tt> module defines a single function to register
cleanup functions. Functions thus registered are automatically
executed upon normal interpreter termination.
<P>
Note: the functions registered via this module are not called when the program is killed by a
signal, when a Python fatal internal error is detected, or when
<tt class="function">os._exit()</tt> is called.
<P>
This is an alternate interface to the functionality provided by the
<code>sys.exitfunc</code> variable.
<P>
Note: This module is unlikely to work correctly when used with other code
that sets <code>sys.exitfunc</code>. In particular, other core Python modules are
free to use <tt class="module">atexit</tt> without the programmer's knowledge. Authors who
use <code>sys.exitfunc</code> should convert their code to use
<tt class="module">atexit</tt> instead. The simplest way to convert code that sets
<code>sys.exitfunc</code> is to import <tt class="module">atexit</tt> and register the function
that had been bound to <code>sys.exitfunc</code>.
<P>
<dl><dt><b><a name='l2h-280'><tt class='function'>register</tt></a></b> (<var>func</var><big>[</big><var>, *args</var><big>[</big><var>, **kargs</var><big>]</big><big>]</big>)
<dd>
Register <var>func</var> as a function to be executed at termination. Any
optional arguments that are to be passed to <var>func</var> must be passed
as arguments to <tt class="function">register()</tt>.
<P>
At normal program termination (for instance, if
<tt class="function">sys.exit()</tt> is called or the main module's execution
completes), all functions registered are called in last in, first out
order. The assumption is that lower level modules will normally be
imported before higher level modules and thus must be cleaned up
later.
</dl>
<P>
<div class='seealso'>
<p class='heading'><b>See Also:</b></p>
<dl compact class="seemodule">
<dt>Module <b><tt class='module'><a href="module-readline.html" tppabs="http://www.python.org/doc/current/lib/module-readline.html">readline</a></tt>:</b>
<dd>Useful example of <tt class="module">atexit</tt> to read and
write <tt class='module'><a href="module-readline.html" tppabs="http://www.python.org/doc/current/lib/module-readline.html">readline</a></tt> history files.
</dl>
</div>
<P>
<p><hr>
<!--Table of Child-Links-->
<A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></A>
<UL>
<LI><A NAME="tex2html1330"
href="atexit-example.html" tppabs="http://www.python.org/doc/current/lib/atexit-example.html">3.3.1 <tt class="module">atexit</tt> Example </A>
</UL>
<!--End of Table of Child-Links-->
<DIV CLASS="navigation"><p><hr><table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A href="module-gc.html" tppabs="http://www.python.org/doc/current/lib/module-gc.html"><img src="previous.gif" tppabs="http://www.python.org/doc/current/icons/previous.gif" border="0" height="32"
alt="Previous Page" width="32"></A></td>
<td><A href="python.html" tppabs="http://www.python.org/doc/current/lib/python.html"><img src="up.gif" tppabs="http://www.python.org/doc/current/icons/up.gif" border="0" height="32"
alt="Up One Level" width="32"></A></td>
<td><A href="atexit-example.html" tppabs="http://www.python.org/doc/current/lib/atexit-example.html"><img src="next.gif" tppabs="http://www.python.org/doc/current/icons/next.gif" border="0" height="32"
alt="Next Page" width="32"></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td><A href="contents.html" tppabs="http://www.python.org/doc/current/lib/contents.html"><img src="contents.gif" tppabs="http://www.python.org/doc/current/icons/contents.gif" border="0" height="32"
alt="Contents" width="32"></A></td>
<td><a href="modindex.html" tppabs="http://www.python.org/doc/current/lib/modindex.html" title="Module Index"><img src="modules.gif" tppabs="http://www.python.org/doc/current/icons/modules.gif" border="0" height="32"
alt="Module Index" width="32"></a></td>
<td><A href="genindex.html" tppabs="http://www.python.org/doc/current/lib/genindex.html"><img src="index.gif" tppabs="http://www.python.org/doc/current/icons/index.gif" border="0" height="32"
alt="Index" width="32"></A></td>
</tr></table>
<b class="navlabel">Previous:</b> <a class="sectref" href="module-gc.html" tppabs="http://www.python.org/doc/current/lib/module-gc.html">3.2 gc </A>
<b class="navlabel">Up:</b> <a class="sectref" href="python.html" tppabs="http://www.python.org/doc/current/lib/python.html">3. Python Runtime Services</A>
<b class="navlabel">Next:</b> <a class="sectref" href="atexit-example.html" tppabs="http://www.python.org/doc/current/lib/atexit-example.html">3.3.1 atexit Example</A>
</DIV>
<!--End of Navigation Panel-->
<ADDRESS>
<hr>See <i><a href="about.html" tppabs="http://www.python.org/doc/current/lib/about.html">About this document...</a></i> for information on suggesting changes.
</ADDRESS>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -