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

📄 113.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; Threads</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="105.html" class="navtitle">A. The Python Library</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="112.html" title="Operating System Services"><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=113" target="_blank" title="Make a public or private annnotation">Make Note</a> | <a href="113.html" title="Use a Safari bookmark to remember this section">Bookmark</a></font></td><td align=right width="15%" class="headingsubbarbg"><a href="114.html" title="Network Programming"><font size="1">CONTINUE&nbsp;&gt;</font></a></td></TR></TABLE>
<a href="5%2F28%2F2002+9%3A06%3A34+PM.html" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><font color=white size=1>155117184014003188065099048180054212144238241179195140058238111161105080005019029193022105</font><a href="read7.asp?bookname=0735710910&snode=113&now=5%2F28%2F2002+9%3A06%3A34+PM" TABINDEX="-1"><img src=images/spacer.gif border=0 width=1 height=1></a><br>
<FONT>
<h3>Threads</h3>
<p>This section describes modules that can be used to develop multithreaded applications. First, a little terminology and background.</p>


<H4>Thread Basics</H4>
<P>A running program is called a <I>process.</i> Associated with each process is a system state including memory, lists of open files, a program counter that keeps track of the instruction being executed, and a call stack used to hold the local variables of functions. Normally, a process executes statements in a single sequence of control flow. This sequence is sometimes called a <i>thread</i> (or <i>main thread</I>).</P>

<P>When a program creates new processes by using the <Tt claSS="monofont">os.system()</TT>, <tt class="monofont">os.fork()</tt>, <tt class="monofont">os.spawnv()</tt>, and similar system calls, these processes run as independent programs, each with its own set of system resources and main thread of execution. However, it抯 also possible for a program to create additional threads of execution that exist inside the calling process and share data and system resources with the original thread of execution. Threads are particularly useful when an application wants to perform tasks concurrently without spawning child processes, or when subtasks need to read and write shared data.</p>

<p>A multithreaded program executes by dividing its processing time between all active threads. For example, a program with 10 active threads of execution would allocate approximately 1/10 of its CPU time to each thread and cycle between threads in rapid succession.</P>

<p>Since threads share the same data, an extreme degree of caution is required whenever shared data structures are updated by one of the threads. In particular,  attempts to update a data structure by multiple threads at approximately the same time can lead to a corrupted and inconsistent program state (a problem formally known as a <i>race condition</I>). To fix these problems, threaded programs need to lock critical sections of code by using mutual-exclusion locks and other similar synchronization primitives.</p>

<p>More information regarding the theory and implementation of threads and locks can be found in most operating system textbooks.</p>


<H4>Python Threads</h4>
<p>Python supports threads on Windows, Solaris, and systems that support the POSIX threads library (<tt ClasS="monofont">pthreads</TT>). However, threads are often disabled by default, so it may be necessary to rebuild the interpreter with thread support before using any of the modules in this section. (Beginning with Python 2.1, thread support is enabled by default.)</P>

<p>The scheduling of threads and thread switching is tightly controlled by a global interpreter lock that allows only a single thread of execution to be running in the interpreter at once. Furthermore, thread switching can only occur between the execution of individual bytecodes in the interpreter. The frequency with which the interpreter checks for thread switching is set by the <tt cLASS="monofont">sys.setcheckinterval()</tt> function. By default, the interpreter checks for thread switching after every 10 bytecode instructions.</p>

<p>When working with extension modules, the interpreter may invoke functions written in C. Unless specifically written to interact with a threaded Python interpreter, these functions block the execution of all other threads until they complete execution. Thus, a long-running calculation in an extension module may limit the effectiveness of using threads. However, most of the I/O functions in the standard library have been written to work in a threaded environment.</P>

<P>Finally, programmers need to be aware that threads can interact strangely with signals and interrupts. For instance, the <TT clasS="monofont">KeyboardInterrupt</TT>  exception can be received by an arbitrary thread, while signals used in conjunction with the <Tt class="monofont">signal</tt>  module are only received by the main thread.</p>

<a name="6"></a>
<h4><tt clAss="monofont">thread</Tt></h4>
<p>The <Tt claSs="monofont">thread</tt>  module provides low-level functions for working with threads. This module is available only on UNIX and Windows.</P>

<PRE>

<b>allocate_lock()</b> </prE>

<P>Creates a new lock object of type <TT clasS="monofont">LockType</TT>. Locks are initially unlocked.</P>

<pre>

<b>exit()</B> </PRE>

<p>Raises the <tt class="monofont">SystemExit</tt>  exception. Forces a thread to exit.</p>

<pre>

<b>get_ident()</b> </pre>

<p>Returns the integer 搕hread identifier

⌨️ 快捷键说明

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