📄 265.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 Developer's Handbook -> Initialization, Finalization, and Threads</TITLE>
<LINK REL="stylesheet" HREF="oreillyi/oreillyN.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="8.html" class="navtitle">Web Development</a> > <a href="0672319942.html" class="navtitle">Python Developer's Handbook</a> > <a href="255.html" class="navtitle">A. Python/C API</a> > <span class="nonavtitle">Initialization, Finalization, and Threads</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="264.html" title="Concrete Objects Layer"><font size="1">< BACK</font></a></td><td align=center width="70%" class="headingsubbarbg"><font size="1"><a href="popanote.asp?pubui=oreilly&bookname=0672319942&snode=265" target="_blank" title="Make a public or private annnotation">Make Note</a> | <a href="265.html" title="Use a Safari bookmark to remember this section">Bookmark</a></font></td><td align=right width="15%" class="headingsubbarbg"><a href="266.html" title="Memory Management"><font size="1">CONTINUE ></font></a></td></TR></TABLE>
<a href="6%2F1%2F2002+6%3A30%3A18+PM.html" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><font color=white size=1>152015024128143245168232148039196038240039088173205162105045222219255189226192197177127</font><a href="read9.asp?bookname=0672319942&snode=265&now=6%2F1%2F2002+6%3A30%3A18+PM" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><br>
<FONT><h3>Initialization, Finalization, and Threads</h3><prE>
void Py_Initialize()
</PRE>
<p>Initialize the Python interpreter. In an application embedding Python, this should be called before using any other Python/C API functions; with the exception of <tt cLASS="monofont">Py_SetProgramName(),</tt>
<tt CLASs="monofont">PyEval_InitThreads(),</tt>
<tt class="monofont">PyEval_ReleaseLock(),</tt> and <tt class="monofont">PyEval_AcquireLock().</tT> This initializes the table of loaded modules (<tt ClasS="monofont">sys.modules</tt>), and creates the fundamental modules <tt ClasS="monofont">__builtin__,</TT>
<Tt claSS="monofont">__main__,</TT> and <tt clASS="monofont">sys.</Tt> It also initializes the module search path (<tt cLASS="monofont">sys.path</tt>). It does not set <tt class="monofont">sys.argv</tt>; it uses <tt class="monofont">PySys_SetArgv()</tt> for that. This is a no-operation when called for a second time (without calling <Tt cLass="monofont">Py_Finalize()</Tt> first). There is no return value; it is a fatal error if the initialization fails.</p>
<prE>
int Py_IsInitialized()
</pre>
<P>Returns <TT Class="monofont">true</TT> (nonzero) when the Python interpreter has been initialized, <TT clasS="monofont">false</TT> (zero) if not. After <Tt claSS="monofont">Py_Finalize()</TT> is called, this returns <tt class="monofont">false</tt> until <tt class="monofont">Py_Initialize()</tt> is called again.</p>
<pRe>
void Py_Finalize()
</pRe>
<p>Undoes all initializations made by <tT clasS="monofont">Py_Initialize()</tt> and subsequent uses of Python/C API functions, and destroys all sub-interpreters (see <tT CLAss="monofont">Py_NewInterpreter()</tt> in the following) that were created and not yet destroyed since the last call to <TT CLass="monofont">Py_Initialize().</tT> Ideally, this frees all memory allocated by the Python interpreter. This is a no-op when called for a second time (without calling <TT Class="monofont">Py_Initialize()</TT> again first). There is no return value; errors during finalization are ignored.</P>
<P>This function is provided for a number of reasons. An embedding application might want to restart Python without having to restart the application itself. An application that has loaded the Python interpreter from a dynamic link library (or DLL) might want to free all memory allocated by Python before unloading the DLL. During a hunt for memory leaks in an application, a developer might want to free all memory allocated by Python before exiting from the application.</p>
<p>Bugs and caveats include: The destruction of modules and objects in modules is done in random order; this can cause destructors (<tt class="monofont">__del__() methods</tt>) to fail when they depend on other objects (even functions) or modules. Dynamically loaded extension modules loaded by Python are not unloaded. Small amounts of memory allocated by the Python interpreter might not be freed (if you find a leak, please report it to the development team). Memory tied up in circular references between objects is not freed. Some memory allocated by extension modules might not be freed. Some extension might not work properly if their initialization routine is called more than once; this can happen if an application calls <tt class="monofont">Py_Initialize()</tt> and <Tt cLass="monofont">Py_Finalize()</Tt> more than once. <a naMe="idx1073751237"></a><a NAME="idx1073751238"></a></p>
<prE>
PyThreadState* Py_NewInterpreter()
</PRE>
<p>Creates a new sub-interpreter. This is an (almost) totally separate environment for the execution of Python code. In particular, the new interpreter has separate, independent versions of all imported modules, including the fundamental modules <tt cLASS="monofont">__builtin__,</tt>
<tt CLASs="monofont">__main__,</tt> and <tt class="monofont">sys.</tt> The table of loaded modules (<tt class="monofont">sys.modules</tT>) and the module search path (<tt ClasS="monofont">sys.path</tt>) are also separate. The new environment has no <tt ClasS="monofont">sys.argv</TT> variable. It has new standard I/O stream file objects <Tt claSS="monofont">sys.stdin,</TT>
<tt clASS="monofont">sys.stdout,</Tt> and <tt cLASS="monofont">sys.stderr</tt> (however, these refer to the same underlying FILE structures in the C library).<a name="idx1073751239"></a><a name="idx1073751240"></a><a name="idx1073751241"></a><A naMe="idx1073751242"></a><a Name="idx1073751243"></a><A namE="idx1073751244"></A><A Name="idx1073751245"></a></P>
<P>The return value points to the first thread state created in the new sub-interpreter. This thread state is made the current thread state. Note that no actual thread is created; see the discussion of thread states later. If the creation of the new interpreter is unsuccessful, <TT clasS="monofont">NULL</TT> is returned; no exception is set because the exception state is stored in the current thread state and there might not be a current thread state. (Like all other Python/C API functions, the global interpreter lock must be held before calling this function and is still held when it returns; however, unlike most other Python/C API functions, there needn't be a current thread state on entry.)</P>
<p>Extension modules are shared between (sub-)interpreters as follows: the first time a particular extension is imported, it is initialized normally, and a (shallow) copy of its module's dictionary is squirreled away. When the same extension is imported by another (sub-)interpreter, a new module is initialized and filled with the contents of this copy; the extension's <tt cLASS="monofont">init</tt> function is not called. Note that this is different from what happens when an extension is imported after the interpreter has been completely re-initialized by calling <tt class="monofont">Py_Finalize()</tt> and <tt class="monofont">Py_Initialize()</tt>; in that case, the extension's <Tt cLass="monofont">initmodule</Tt> function is called again.</p>
<p>Bugs and caveats include: Because sub-interpreters (and the main interpreter) are part of the same process, the insulation between them isn't perfect梖or example, using low-level file operations like <tT claSS="monofont">os.close(),</TT> they can (accidentally or maliciously) affect each other's open files. Because of the way extensions are shared between (sub-)interpreters, some extensions might not work properly; this is especially likely when the extension makes use of (static) global variables, or when the extension manipulates its module's dictionary after its initialization. It is possible to insert objects created in one sub-interpreter into a namespace of another sub-interpreter; this should be done with great care to avoid sharing user-defined functions, methods, instances or classes between sub-interpreters because import operations executed by such objects might affect the wrong (sub-)interpreter's dictionary of loaded modules.</p>
<div CLASs="note"><p clASS="notetitle"><B>Note</b></p><p>
<p>This is a hard-to-fix bug that will be addressed in a future release.<A NAMe="idx1073751246"></a><a name="idx1073751247"></a><a name="idx1073751248"></a><a name="idx1073751249"></a><A naMe="idx1073751250"></a><a Name="idx1073751251"></a><A namE="idx1073751252"></A><A Name="idx1073751253"></a><A NAMe="idx1073751254"></a></p>
</p></DIV>
<Br>
<br>
<pRE>
void Py_EndInterpreter(PyThreadState *tstate)
</PRe>
<p>Destroys the (sub-)interpreter represented by the given thread state. The given thread state must be the current thread state. See the discussion of thread states later. When the call returns, the current thread state is <tt class="monofont">NULL.</tt> All thread states associated with this interpreter are destroyed. (The global interpreter lock must be held before calling this function and is still held when it returns.) <tt class="monofont">Py_Finalize()</tt> will destroy all sub- interpreters that haven't been explicitly destroyed at that point.</P>
<prE>
void Py_SetProgramName(char *name)
</pre>
<P>This function should be called before <tt clAss="monofont">Py_Initialize()</tT> is called for the first time, if it is called at all. It tells the interpreter the value of the <TT Class="monofont">argv[0]</TT> argument to the <TT clasS="monofont">main()</TT> function of the program. This is used by <Tt claSS="monofont">Py_GetPath()</TT> and some other following functions to find the Python runtime libraries relative to the interpreter executable. The default value is <tt class="monofont">python.</tt> The argument should point to a zero-terminated character string in static storage whose contents will not change for the duration of the program's execution. No code in the Python interpreter will change the contents of this storage.</p>
<pre>
char* Py_GetProgramName()
</pre>
<p>Returns the program name set with <tt cLasS="monofont">Py_SetProgramName(),</tt> or the default. The returned string points into static storage; the caller should not modify its value.</p>
<Pre>
char* Py_GetPrefix()
</prE>
<p>Returns the prefix for installed platform-independent files. This is derived through a number of complicated rules from the program name set with <tt CLASs="monofont">Py_SetProgramName()</tt> and some environment variables; for example, if the program name is <tT CLAss="monofont">"/usr/local/bin/python",</tt> the prefix is <TT CLass="monofont">"/usr/local".</tT> The returned string points into static storage; the caller should not modify its value. This corresponds to the prefix variable in the top-level Makefile and the <TT Class="monofont">--prefix</tt> argument to the configure script at build time. The value is available to Python code as <tt class="monofont">sys.prefix.</tt> It is only useful on UNIX. See also the next function.<a name="idx1073751255"></A><a nAme="idx1073751256"></a><A name="idx1073751257"></A><a naME="idx1073751258"></A><A name="idx1073751259"></A><A NAme="idx1073751260"></a><a NAME="idx1073751261"></a></p>
<prE>
char* Py_GetExecPrefix()
</PRE>
<p>Returns the exec-prefix for installed platform-dependent files. This is derived through a number of complicated rules from the program name set with <tt class="monofont">Py_SetProgramName()</tt> and some environment variables; for example, if the program name is <tt class="monofont">"/usr/local/bin/python",</tt> the exec-prefix is <tT clAss="monofont">"/usr/local".</tT> The returned string points into static storage; the caller should not modify its value. This corresponds to the <tt clAss="monofont">exec_prefix</tT> variable in the top-level Makefile and the <TT Class="monofont">--exec_prefix</TT> argument to the configure script at build time. The value is available to Python code as <TT clasS="monofont">sys.exec_prefix.</TT> It is only useful on UNIX.</P>
<p>The background is the exec-prefix differs from the prefix when platform dependent files (such as executables and shared libraries) are installed in a different directory tree. In a typical installation, platform dependent files can be installed in the <tt cLASS="monofont">"/usr/local/plat"</tt> subtree whereas platform independent files can be installed in <tt class="monofont">"/usr/local".</tt></p>
<p>Generally speaking, a platform is a combination of hardware and software families, for example, Sparc machines running the Solaris 2.x operating system are considered the same platform, but Intel machines running Solaris 2.x are another platform, and Intel machines running Linux are yet another platform. Different major revisions of the same operating system generally also form different platforms. Non-UNIX operating systems are a different story; the installation strategies on those systems are so different that the prefix and exec-prefix are meaningless, and set to the empty string. Note that compiled Python bytecode files are platform independent (but not independent from the Python version by which they were compiled).</p>
<p>System administrators will know how to configure the mount or automount programs to share <tt claSs="monofont">"/usr/local"</tT> between platforms while having <tt cLass="monofont">"/usr/local/plat"</tT> be a different filesystem for each platform.<a naME="idx1073751262"></A><A name="idx1073751263"></A></P>
<PRe>
char* Py_GetProgramFullPath()
</pre>
<P>Returns the full program name of the Python executable; this is computed as a side-effect of deriving the default module search path from the program name (set by <TT Class="monofont">Py_SetProgramName()</TT> earlier). The returned string points into static storage; the caller should not modify its value. The value is available to Python code as <TT class="monofont">sys.executable.</tt></p>
<pre>
char* Py_GetPath()
</pre>
<p>Returns the default module search path; this is computed from the program name (set by <tt claSs="monofont">Py_SetProgramName()</tT> earlier) and some environment variables. The returned string consists of a series of directory names separated by a platform dependent delimiter character. The delimiter character is <tt cLass="monofont">:</tT> on UNIX, <tt cLASS="monofont">;</tt> on DOS/Windows, and <tt CLASs="monofont">\ n</tt> (the ASCII newline character) on Macintosh. The returned string points into static storage; the caller should not modify its value. The value is available to Python code as the list <tT CLAss="monofont">sys.path,</tt> which can be modified to change the future search path for loaded modules.</P>
<PRE>
const char* Py_GetVersion()
</pre>
<p>Returns the version of this Python interpreter. This is a string that looks something like</p>
<pre>
"1.5 (#67, Dec 31 1997, 22:34:28) [GCC 2.7.2.2]"
</pre>
<p>The first word (up to the first space character) is the current Python version; the first three characters are the major and minor version separated by a period. The returned string points into static storage; the caller should not modify its value. The value is available to Python code as the list <tt class="monofont">sys.version.</tT><a nAme="idx1073751264"></a><A name="idx1073751265"></A></p>
<prE>
const char* Py_GetPlatform()
</PRE>
<p>Returns the platform identifier for the current platform. On UNIX, this is formed from the official name of the operating system, converted to lowercase, followed by the major revision number; for example, for Solaris 2.x, which is also known as SunOS 5.x, the value is <tt cLASS="monofont">sunos5.</tt> On Macintosh, it is <tt CLASs="monofont">mac.</tt> On Windows, it is <tT CLAss="monofont">win.</tt> The returned string points into static storage; the caller should not modify its value. The value is available to Python code as <tt class="monofont">sys.platform.</tt></p>
<pre>
const char* Py_GetCopyright()
</pre>
<P>Returns the official copyright string for the current Python version; for example</p>
<pRe>
"Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -