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

📄 156.html

📁 Python Ebook Python&XML
💻 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 -&gt; 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> &gt; <a href="0672319942.html" class="navtitle">Python Developer's Handbook</a> &gt; <a href="148.html" class="navtitle">9. Other Advanced Topics</a> &gt; <span class="nonavtitle">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="155.html" title="Regular Expressions"><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=0672319942&snode=156" target="_blank" title="Make a public or private annnotation">Make Note</a> | <a href="156.html" title="Use a Safari bookmark to remember this section">Bookmark</a></font></td><td align=right width="15%" class="headingsubbarbg"><a href="157.html" title="Summary"><font size="1">CONTINUE&nbsp;&gt;</font></a></td></TR></TABLE>
<a href="5%2F31%2F2002+4%3A40%3A55+PM.html" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><font color=white size=1>152015024128143245168232148039199167010047123209178152124239215162148044238001160165056249</font><a href="read6.asp?bookname=0672319942&snode=156&now=5%2F31%2F2002+4%3A40%3A55+PM" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><br>
<FONT>
				<h3>
			Threads</h3>
				<p>Let's start by quickly defining a thread. Many people still have some kind of confusion when it comes to clarifying the difference between threads and processes.</p>

				<P>When you run any program in your computer, the CPU creates a process for that program. This process is defined as a group of elements that compound a single program. These elements are the memory area reserved for the program, a program counter, a list of files opened by the program, and a call stack where all the variables are stored. A program with a single call stack and program counter is a single threaded program.</P>

				<P>Now, suppose you have different tasks inside your program that you need to execute several times simultaneously. What do you do? Maybe you are thinking about calling the whole program several times. Wrong answer! Think about all the resources that you are consuming without actually using them!<A name="idx1073745601"></A>
				</P>

				<P>The solution to implement this multithreaded program is to create a function that implements the code which needs to be executed several times concurrently, and then, create a thread that uses only this function.</P>

				<p>A <i>thread</i> is a program unit that processes multiple time-consuming actions as parallel tasks in the background of your main application process. Sometimes threads are difficult to debug because the circumstances in which they occur are hard to simulate.</p>

				
					<H4>Python Threads</H4>
					<P>Python threads can be implemented on every operational system that supports the POSIX threads library. But actually, the Python threading support doesn't always use POSIX threads. In the python-2.0 source tree, there are beos, cthread, lwp, nt, os2, pth, pthread, sgi, solaris, and wince thread implementations. In certain environments that support multithreading, Python allows the interpreter to run many threads at once.</P>

					<p>Python has two threading interfaces: The <tt class="monofont">thread</tt> module and the <tt class="monofont">threading</tt> module. The use of these Python's native threading built-in modules enables the code to be portable across all platforms that support Python.</p>

					<P>The <tt ClasS="monofont">thread</tt> module supports lightweight process threads. It offers a low-level interface for working with multiple threads.</p>

					<p>On the other hand, the <Tt clASS="monofont">threading</Tt> module provides high-level threading interfaces on top of the <tt cLASS="monofont">thread</tt> module.</p>

					<p>Besides these two modules, Python also implements the <TT CLass="monofont">Queue</tT> module. This is a synchronized <A NAme="idx1073745602"></a>
						<a name="idx1073745603"></a>
						<i>queue class</i> used in thread programming to move Python objects between multiple threads in a safe way.</p>

					<p>Threads have limitations on some platforms. For instance, Linux thread switching is quite fast, sometimes faster than NT thread switching.</p>

					<p>Programs梥uch as Tkinter, CORBA, and ILU梩hat rely on a main loop to dispatch events can complicate the design of threads. Definitively, they do not have a good relationship with threaded programs. Main loops are usually used by Graphical User Interfaces not to allow the main thread to exit.</p>

					<p>
						<a naMe="idx1073745604"></a>
						<A namE="idx1073745605"></a>
						<a naMe="idx1073745606"></a>
						<a NAME="idx1073745607"></a>MacPython is currently not built with thread support. That is because no posix-compatible thread implementation was available, making Python integration hard. However, this has changed with GUSI2 (a <i>posix</i> I/O emulation library), and the upcoming MacPython 1.6a1 is planned to have threads.</p>

					<P>The <A NAme="idx1073745608"></a>Windows Operation System adds many additional features to Python's implementation of threads. The win32 package provides as additional features for Python's thread support:<a NAME="idx1073745609"></a>
					</p>

					<ul>
<LI>
							<P>The <Tt class="monofont">win32process</tt> module桝n interface to the win32 Process and Thread API's.</p>

						</li>
<li>
							<p>The <tt clasS="monofont">win32event</tt> module桝 module that provides an interface to the win32 event/wait API.</P>

						</li>
</uL>
					<p>The threading model provided by the COM technology allows objects not designed to work as threads to be used by other objects that are thread-aware.</p>

					<p>Python's <a Name="idx1073745610"></A>
						<A NAme="idx1073745611"></a>interpreter cannot handle more than one thread at the same time. The global interpreter lock is the internal mechanism which guarantees that the Python interpreter executes only one thread simultaneously. Although this is not a problem for single-threaded programs, or programs on single-processor machines, it can become trouble on performance-critical applications that run on multiprocessor computers. If your threads are doing IO work, other threads can execute during reads and writes.</p>

					<P>Check out <A HRef="255.html">Appendix A, "Python/C API,"
						</a>  for information about handling threads using the Python/C API. You can also see the latest documentation about it at</p>

					<PRE>
						
					<A targET="_blank" HRef="http://www.python.org/doc/current/api/threads.html">http://www.python.org/doc/current/api/threads.html</a>
				
					</pre>

					<p>You might also want to look at the <tt class="monofont">thread</tt> and <tt clAss="monofont">threading</Tt> modules in the library reference, which are documented at</p>

					<pRe>
						
					<a taRget="_blank" HREF="http://www.python.org/doc/current/lib/module-thread.html">http://www.python.org/doc/current/lib/module-thread.html</a>
				
					</pre>

					<P>and</P>

					<PRe>
						
					<a taRGET="_blank" href="http://www.python.org/doc/current/lib/module-threading.html">http://www.python.org/doc/current/lib/module-threading.html</A>
				
					</PRE>

					<p>Anton Ertl has a Web page that exposes very interesting material about the differences between the various threading techniques:</p>

					<pre>
						
					<a target="_blank" href="http://www.complang.tuwien.ac.at/forth/threaded-code.html">http://www.complang.tuwien.ac.at/forth/threaded-code.html</a>
				
					</pre>

				
				
					<H4>
				
				Python Thread Modules</h4>
					<p>Python includes two threading modules, assuming that your Python was configured for threads when it was built. One provides the primitives, and the other provides higher-level access. In general, Python relies on operating system threads unless you specifically compile it by activating the thread directive. This should offer adequate performance for all but the most demanding applications.</P>

					
						<h5>Thread Module</h5>
						<p>The following four functions are available in this module:</P>

						<ul>
<li>
								<P>
									
										<tt cLASS="monofont">thread.allocate_lock()

⌨️ 快捷键说明

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