Sophie

Sophie

distrib > Mandriva > 8.2 > i586 > by-pkgid > 0b7eb7009605a11593fbe388d7fbee61 > files > 562

python-docs-2.2-9.1mdk.i586.rpm

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>7.5 threading -- Higher-level threading interface</title>
<META NAME="description" CONTENT="7.5 threading -- Higher-level threading interface">
<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=">
<link rel="STYLESHEET" href="lib.css">
<link rel="first" href="lib.html">
<link rel="contents" href="contents.html" title="Contents">
<link rel="index" href="genindex.html" title="Index">
<LINK REL="next" href="module-Queue.html">
<LINK REL="previous" href="module-thread.html">
<LINK REL="up" href="someos.html">
<LINK REL="next" href="lock-objects.html">
</head>
<body>
<DIV CLASS="navigation">
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A href="module-thread.html"><img src="../icons/previous.gif"
  border="0" height="32"
  alt="Previous Page" width="32"></A></td>
<td><A href="someos.html"><img src="../icons/up.gif"
  border="0" height="32"
  alt="Up One Level" width="32"></A></td>
<td><A href="lock-objects.html"><img src="../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"><img src="../icons/contents.gif"
  border="0" height="32"
  alt="Contents" width="32"></A></td>
<td><a href="modindex.html" title="Module Index"><img src="../icons/modules.gif"
  border="0" height="32"
  alt="Module Index" width="32"></a></td>
<td><A href="genindex.html"><img src="../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-thread.html">7.4 thread  </A>
<b class="navlabel">Up:</b> <a class="sectref" href="someos.html">7. Optional Operating System</A>
<b class="navlabel">Next:</b> <a class="sectref" href="lock-objects.html">7.5.1 Lock Objects</A>
<br><hr>
</DIV>
<!--End of Navigation Panel-->

<H1><A NAME="SECTION009500000000000000000">
7.5 <tt class="module">threading</tt> --
         Higher-level threading interface</A>
</H1>

<P>


<P>
This module constructs higher-level threading interfaces on top of the 
lower level <tt class="module"><a href="module-thread.html">thread</a></tt> module.

<P>
This module is safe for use with "<tt class="samp">from threading import *</tt>".  It
defines the following functions and objects:

<P>
<dl><dt><b><a name="l2h-1943"><tt class="function">activeCount</tt></a></b>()
<dd>
Return the number of currently active <tt class="class">Thread</tt> objects.
The returned count is equal to the length of the list returned by
<tt class="function">enumerate()</tt>.
A function that returns the number of currently active threads.
</dl>

<P>
<dl><dt><b><a name="l2h-1944"><tt class="function">Condition</tt></a></b>()
<dd>
A factory function that returns a new condition variable object.
A condition variable allows one or more threads to wait until they
are notified by another thread.
</dl>

<P>
<dl><dt><b><a name="l2h-1945"><tt class="function">currentThread</tt></a></b>()
<dd>
Return the current <tt class="class">Thread</tt> object, corresponding to the
caller's thread of control.  If the caller's thread of control was not
created through the
<tt class="module">threading</tt> module, a dummy thread object with limited functionality
is returned.
</dl>

<P>
<dl><dt><b><a name="l2h-1946"><tt class="function">enumerate</tt></a></b>()
<dd>
Return a list of all currently active <tt class="class">Thread</tt> objects.
The list includes daemonic threads, dummy thread objects created
by <tt class="function">currentThread()</tt>, and the main thread.  It excludes terminated
threads and threads that have not yet been started.
</dl>

<P>
<dl><dt><b><a name="l2h-1947"><tt class="function">Event</tt></a></b>()
<dd>
A factory function that returns a new event object.  An event
manages a flag that can be set to true with the <tt class="method">set()</tt> method and
reset to false with the <tt class="method">clear()</tt> method.  The <tt class="method">wait()</tt> method blocks
until the flag is true.
</dl>

<P>
<dl><dt><b><a name="l2h-1948"><tt class="function">Lock</tt></a></b>()
<dd>
A factory function that returns a new primitive lock object.  Once
a thread has acquired it, subsequent attempts to acquire it block,
until it is released; any thread may release it.
</dl>

<P>
<dl><dt><b><a name="l2h-1949"><tt class="function">RLock</tt></a></b>()
<dd>
A factory function that returns a new reentrant lock object.
A reentrant lock must be released by the thread that acquired it.
Once a thread has acquired a reentrant lock, the same thread may
acquire it again without blocking; the thread must release it once
for each time it has acquired it.
</dl>

<P>
<dl><dt><b><a name="l2h-1950"><tt class="function">Semaphore</tt></a></b>(<big>[</big><var>value</var><big>]</big>)
<dd>
A factory function that returns a new semaphore object.  A
semaphore manages a counter representing the number of <tt class="method">release()</tt>
calls minus the number of <tt class="method">acquire()</tt> calls, plus an initial value.
The <tt class="method">acquire()</tt> method blocks if necessary until it can return
without making the counter negative.  If not given, <var>value</var> defaults to
1. 
</dl>

<P>
<dl><dt><b><a name="l2h-1951"><tt class="function">BoundedSemaphore</tt></a></b>(<big>[</big><var>value</var><big>]</big>)
<dd>
A factory function that returns a new bounded semaphore object.  A bounded
semaphore checks to make sure its current value doesn't exceed its initial
value.  If it does, <tt class="exception">ValueError</tt> is raised. In most situations
semaphores are used to guard resources with limited capacity.  If the
semaphore is released too many times it's a sign of a bug.  If not given,
<var>value</var> defaults to 1. 
</dl>

<P>
<dl><dt><b><span class="typelabel">class</span> <a name="l2h-1952"><tt class="class">Thread</tt></a></b>
<dd>
A class that represents a thread of control.  This class can be safely subclassed in a limited fashion.
</dl>

<P>
<dl><dt><b><span class="typelabel">class</span> <a name="l2h-1953"><tt class="class">Timer</tt></a></b>
<dd>
A thread that executes a function after a specified interval has passed.
</dl>

<P>
Detailed interfaces for the objects are documented below.  

<P>
The design of this module is loosely based on Java's threading model.
However, where Java makes locks and condition variables basic behavior
of every object, they are separate objects in Python.  Python's <tt class="class">Thread</tt>
class supports a subset of the behavior of Java's Thread class;
currently, there are no priorities, no thread groups, and threads
cannot be destroyed, stopped, suspended, resumed, or interrupted.  The
static methods of Java's Thread class, when implemented, are mapped to
module-level functions.

<P>
All of the methods described below are executed atomically.

<P>

<p><hr>
<!--Table of Child-Links-->
<A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></a>

<UL CLASS="ChildLinks">
<LI><A href="lock-objects.html">7.5.1 Lock Objects</a>
<LI><A href="rlock-objects.html">7.5.2 RLock Objects</a>
<LI><A href="condition-objects.html">7.5.3 Condition Objects</a>
<LI><A href="semaphore-objects.html">7.5.4 Semaphore Objects</a>
<UL>
<LI><A href="semaphore-examples.html">7.5.4.1 <tt class="class">Semaphore</tt> Example</a>
</ul>
<LI><A href="event-objects.html">7.5.5 Event Objects</a>
<LI><A href="thread-objects.html">7.5.6 Thread Objects</a>
<LI><A href="timer-objects.html">7.5.7 Timer Objects</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-thread.html"><img src="../icons/previous.gif"
  border="0" height="32"
  alt="Previous Page" width="32"></A></td>
<td><A href="someos.html"><img src="../icons/up.gif"
  border="0" height="32"
  alt="Up One Level" width="32"></A></td>
<td><A href="lock-objects.html"><img src="../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"><img src="../icons/contents.gif"
  border="0" height="32"
  alt="Contents" width="32"></A></td>
<td><a href="modindex.html" title="Module Index"><img src="../icons/modules.gif"
  border="0" height="32"
  alt="Module Index" width="32"></a></td>
<td><A href="genindex.html"><img src="../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-thread.html">7.4 thread  </A>
<b class="navlabel">Up:</b> <a class="sectref" href="someos.html">7. Optional Operating System</A>
<b class="navlabel">Next:</b> <a class="sectref" href="lock-objects.html">7.5.1 Lock Objects</A>
<hr>
<span class="release-info">Release 2.2, documentation updated on December 21, 2001.</span>
</DIV>
<!--End of Navigation Panel-->
<ADDRESS>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</ADDRESS>
</BODY>
</HTML>