⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 104.html

📁 国外python经典教材,python爱好者的首选
💻 HTML
字号:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Robots" content="INDEX,NOFOLLOW">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<TITLE>Safari | Python Essential Reference, Second Edition -&gt; Program Termination</TITLE>
<LINK REL="stylesheet" HREF="oreillyi/oreillyM.css">
</HEAD>
<BODY bgcolor="white" text="black" link="#990000" vlink="#990000" alink="#990000" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">

<table width="100%" cellpadding=5 cellspacing=0 border=0 class="navtopbg"><tr><td><font size="1"><p class="navtitle"><a href="2.html" class="navtitle">Linux/Unix</a> &gt; <a href="0735710910.html" class="navtitle">Python Essential Reference, Second Edition</a> &gt; <a href="97.html" class="navtitle">10. Execution Environment</a> &gt; <span class="nonavtitle">Program Termination</span></p></font></td><td align="right" valign="top" nowrap><font size="1"><a href="main.asp?list" class="safnavoff">See All Titles</a></font></td></tr></table>
<TABLE width=100% bgcolor=white border=0 cellspacing=0 cellpadding=5><TR><TD>
<TABLE border=0 width="100%" cellspacing=0 cellpadding=0><TR><td align=left width="15%" class="headingsubbarbg"><a href="103.html" title="Enabling Future Features"><font size="1">&lt;&nbsp;BACK</font></a></td><td align=center width="70%" class="headingsubbarbg"><font size="1"><a href="popanote.asp?pubui=oreilly&bookname=0735710910&snode=104" target="_blank" title="Make a public or private annnotation">Make Note</a> | <a href="104.html" title="Use a Safari bookmark to remember this section">Bookmark</a></font></td><td align=right width="15%" class="headingsubbarbg"><a href="106.html" title="A. The Python Library"><font size="1">CONTINUE&nbsp;&gt;</font></a></td></TR></TABLE>
<a href="5%2F28%2F2002+9%3A04%3A35+PM.html" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><font color=white size=1>155117184014003188065099048180054212144238241179195140058238111161105082191050116112014099</font><a href="read1.asp?bookname=0735710910&snode=104&now=5%2F28%2F2002+9%3A04%3A35+PM" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><br>
<FONT>
<h3>Program Termination</h3>
<p>A program terminates when no more statements exist to execute in the input program, when an uncaught <tT CLAss="monofont">SystemExit</tt>  exception is raised (as generated by <TT CLass="monofont">sys.exit()</tT>), or when the interpreter receives a <TT Class="monofont">SIGTERM</tt>  or <tt class="monofont">SIGHUP</tt>  signal (on UNIX). On exit, the interpreter decrements the reference count of all objects in all the currently known namespaces (and destroys each namespace as well). If the reference count of an object reaches zero, the object is destroyed and its <tt claSs="monofont">_ _ del_ _ ()</tT> method is invoked. It抯 important to note that in some cases the <tt cLass="monofont">_ _ del_ _ ()</tT> method might not be invoked at program termination. This can occur if circular references exist between objects (in which case objects may be allocated, but accessible from no known namespace). Although Python抯 garbage collector can reclaim unused circular references during execution, it isn抰 normally invoked on program termination.</p>

<p>Because there抯 no guarantee that <tT CLAss="monofont">_ _ del_ _ ()</tt> will be invoked at termination, it may be a good idea to explicitly clean up certain objects, such as open files and network connections. To accomplish this, add specialized cleanup methods (for example, <TT CLass="monofont">close()</tT>) to user-defined objects. Another possibility is to write a termination function and register it with the <TT Class="monofont">atexit</TT>  module, as follows:</P>

<Pre>

import atexit 
connection = open_connection("deaddot.com") 

def cleanup(): 
    print "Going away..." 
    close_connection(connection) 

atexit.register(cleanup) </pre>

<p>The garbage collector can also be invoked in this manner:</p>

<pre>

import atexit, gc 
atexit.register(gc.collect) </pre>

<p>One final peculiarity about program termination is that the <tt clasS="monofont">_ _ del_ _</tt> method for some objects may try to access global data or methods defined in other modules. Since these objects may already have been destroyed, a <Tt clAss="monofont">NameError</tt>  exception occurs in <Tt clASS="monofont">_ _ del_ _</Tt>, and you may get an error such as the following:</p>

<prE>

Exception exceptions.NameError: 'c' in &lt;method Bar._ _ del_ _ of Bar instance at c0310&gt; 
<IMG bordER="0" ALign="left" wIDTH="14" height="9" src="graphics/ccc.gif" alt="graphics/ccc.gif">ignored
</pre>

<p>If this occurs, it means that <tt clAss="monofont">_ _ del_ _</Tt> has aborted prematurely. It also implies that it may have failed in an attempt to perform an important operation (such as cleanly shutting down a server connection). If this is a concern, it抯 probably a good idea to perform an explicit shutdown step in your code, rather than relying on the interpreter to destroy objects cleanly at program termination. The peculiar <tt Class="monofont">NameError</Tt>  exception can also be eliminated by declaring default arguments in the declaration of the <tt CLASs="monofont">_ _ del_ _ ()</tt> method:</p>

<PRE>

import foo 
class Bar: 
   def _ _ del_ _ (self, foo=foo): 
      foo.bar()        # Use something in module foo </Pre>

<p>In some cases, it may be useful to terminate program execution without performing any cleanup actions. This can be accomplished by calling <tT CLAss="monofont">os._exit(</tt><I><TT Class="monofont">status</tt></i>
<tt class="monofont">)</tt>. This function provides an interface to the low-level <tt clAss="monofont">exit()</Tt> system call responsible for killing the Python interpreter process.When invoked, the program immediately terminates.</p>
</fOnt>
<P><TABLE width="100%" border=0><TR valign="top"><TD><font size=1 color="#C0C0C0"><br></font></TD><TD align=right><font size=1 color="#C0C0C0">Last updated on 3/28/2002<br>Python Essential Reference, Second Edition, &copy;&nbsp;2002 New Riders Publishing</font></TD></TR></TABLE></P>
<TABLE border=0 width="100%" cellspacing=0 cellpadding=0><TR><td align=left width="15%" class="headingsubbarbg"><a href="103.html" title="Enabling Future Features"><font size="1">&lt;&nbsp;BACK</font></a></td><td align=center width="70%" class="headingsubbarbg"><font size="1"><a href="popanote.asp?pubui=oreilly&bookname=0735710910&snode=104" target="_blank" title="Make a public or private annnotation">Make Note</a> | <a href="104.html" title="Use a Safari bookmark to remember this section">Bookmark</a></font></td><td align=right width="15%" class="headingsubbarbg"><a href="106.html" title="A. The Python Library"><font size="1">CONTINUE&nbsp;&gt;</font></a></td></TR></TABLE>
</TD></TR></TABLE>




<!--EndOfBrowse-->

</TD></TR></TABLE>
<table width=100% border=0 cellspacing=0 cellpadding=0 bgcolor=#990000><tr><td><p align=center><font size=1 face="verdana,arial,helvetica" color=white>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -