<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!--Converted with LaTeX2HTML 98.2 beta6 (August 14th, 1998)
original version by:  Nikos Drakos, CBLU, University of Leeds
* revised and updated by:  Marcus Hennecke, Ross Moore, Herb Swan
* with significant contributions from:
  Jens Lippmann, Marek Rouchal, Martin Wilck and others -->
<HTML>
<HEAD>
<TITLE>7.4 thread --
Multiple threads of control</TITLE>
<META NAME="description" CONTENT="7.4 thread --
Multiple threads of control">
<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=iso-8859-1">
<LINK REL="STYLESHEET" HREF="lib.css">
<LINK REL="next" HREF="module-threading.html">
<LINK REL="previous" HREF="module-select.html">
<LINK REL="up" HREF="someos.html">
<LINK REL="next" HREF="module-threading.html">
</HEAD>
<BODY bgcolor="#ffffff">
<div class=navigation>
<table align=center width="100%" cellpadding=0 cellspacing=2>
<tr>
<td><A HREF="module-threading.html"><img width=32 height=32 align=bottom border=0 alt="next"
 src="../icons/next.gif"></A></td>
<td><A HREF="someos.html"><img width=32 height=32 align=bottom border=0 alt="up"
 src="../icons/up.gif"></A></td>
<td><A HREF="module-select.html"><img width=32 height=32 align=bottom border=0 alt="previous"
 src="../icons/previous.gif"></A></td>
<td align=center bgcolor="#99CCFF" width="100%">
 <b class=title>Python Library Reference</b></td>
<td><A HREF="contents.html"><img width=32 height=32 align=bottom border=0 alt="contents"
 src="../icons/contents.gif"></A></td>
<td><a
 href="modindex.html"><img width=32 height=32 align=bottom border=0 alt="modules"
 src="../icons/modules.gif"></a></td>
<td><A HREF="genindex.html"><img width=32 height=32 align=bottom border=0 alt="index"
 src="../icons/index.gif"></A></td>
</tr></table><b class=navlabel>Next:</b> <span class=sectref><A HREF="module-threading.html">7.5 threading  </A></span>
<b class=navlabel>Up:</b> <span class=sectref><A HREF="someos.html">7. Optional Operating System</A></span>
<b class=navlabel>Previous:</b> <span class=sectref><A HREF="module-select.html">7.3 select  </A></span>
<br><hr></div>
<!--End of Navigation Panel-->

<H1><A NAME="SECTION009400000000000000000">
7.4 <tt class=module>thread</tt> --
         Multiple threads of control</A>
</H1>

<P>


<P>
This module provides low-level primitives for working with multiple
threads (a.k.a. <i class=dfn>light-weight processes</i> or <i class=dfn>tasks</i>) -- multiple
threads of control sharing their global data space.  For
synchronization, simple locks (a.k.a. <i class=dfn>mutexes</i> or <i class=dfn>binary
semaphores</i>) are provided.
<a name="l2h-1330"></a>
<a name="l2h-1331"></a>
<a name="l2h-1332"></a>
<a name="l2h-1333"></a>

<P>
The module is optional.  It is supported on Windows NT and '95, SGI
IRIX, Solaris 2.x, as well as on systems that have a POSIX thread
(a.k.a. ``pthread'') implementation.
<a name="l2h-1334"></a>
<a name="l2h-1335"></a>

<P>
It defines the following constant and functions:

<P>
<dl><dt><b><a name="l2h-1320"><tt class=exception>error</tt></a></b>
<dd>
Raised on thread-specific errors.
</dl>

<P>
<dl><dt><b><a name="l2h-1321"><tt>LockType</tt></a></b>
<dd>
This is the type of lock objects.
</dl>

<P>
<dl><dt><b><a name="l2h-1322"><tt class=function>start_new_thread</tt></a></b> (<var>function, args</var><big>[</big><var>, kwargs</var><big>]</big>)
<dd>
Start a new thread.  The thread executes the function <var>function</var>
with the argument list <var>args</var> (which must be a tuple).  The
optional <var>kwargs</var> argument specifies a dictionary of keyword
arguments.  When the
function returns, the thread silently exits.  When the function
terminates with an unhandled exception, a stack trace is printed and
then the thread exits (but other threads continue to run).
</dl>

<P>
<dl><dt><b><a name="l2h-1323"><tt class=function>exit</tt></a></b> ()
<dd>
Raise the <tt class=exception>SystemExit</tt> exception.  When not caught, this
will cause the thread to exit silently.
</dl>

<P>
<dl><dt><b><a name="l2h-1324"><tt class=function>exit_thread</tt></a></b> ()
<dd>
<b>Deprecated since release 1.5.2.</b>
Use <tt class=function>exit()</tt>.<p>
This is an obsolete synonym for <tt class=function>exit()</tt>.
</dl>

<P>
<dl><dt><b><a name="l2h-1325"><tt class=function>allocate_lock</tt></a></b> ()
<dd>
Return a new lock object.  Methods of locks are described below.  The
lock is initially unlocked.
</dl>

<P>
<dl><dt><b><a name="l2h-1326"><tt class=function>get_ident</tt></a></b> ()
<dd>
Return the `thread identifier' of the current thread.  This is a
nonzero integer.  Its value has no direct meaning; it is intended as a
magic cookie to be used e.g. to index a dictionary of thread-specific
data.  Thread identifiers may be recycled when a thread exits and
another thread is created.
</dl>

<P>
Lock objects have the following methods:

<P>
<dl><dt><b><a name="l2h-1327"><tt class=method>acquire</tt></a></b> (<big>[</big><var>waitflag</var><big>]</big>)
<dd>
Without the optional argument, this method acquires the lock
unconditionally, if necessary waiting until it is released by another
thread (only one thread at a time can acquire a lock -- that's their
reason for existence), and returns <tt>None</tt>.  If the integer
<var>waitflag</var> argument is present, the action depends on its
value: if it is zero, the lock is only acquired if it can be acquired
immediately without waiting, while if it is nonzero, the lock is
acquired unconditionally as before.  If an argument is present, the
return value is <tt>1</tt> if the lock is acquired successfully,
<tt>0</tt> if not.
</dl>

<P>
<dl><dt><b><a name="l2h-1328"><tt class=method>release</tt></a></b> ()
<dd>
Releases the lock.  The lock must have been acquired earlier, but not
necessarily by the same thread.
</dl>

<P>
<dl><dt><b><a name="l2h-1329"><tt class=method>locked</tt></a></b> ()
<dd>
Return the status of the lock: <tt>1</tt> if it has been acquired by
some thread, <tt>0</tt> if not.
</dl>

<P>
<b>Caveats:</b>

<P>

<UL><LI>Threads interact strangely with interrupts: the
<tt class=exception>KeyboardInterrupt</tt> exception will be received by an
arbitrary thread.  (When the <tt class=module><a href="module-signal.html">signal</a></tt><a name="l2h-1336"></a>
module is available, interrupts always go to the main thread.)

<P>

<LI>Calling <tt class=function>sys.exit()</tt> or raising the <tt class=exception>SystemExit</tt>
exception is equivalent to calling <tt class=function>exit_thread()</tt>.

<P>

<LI>Not all built-in functions that may block waiting for I/O allow other
threads to run.  (The most popular ones (<tt class=function>time.sleep()</tt>,
<tt class=method><var>file</var>.read()</tt>, <tt class=function>select.select()</tt>) work as
expected.)

<P>

<LI>It is not possible to interrupt the <tt class=method>acquire()</tt> method on a lock
-- the <tt class=exception>KeyboardInterrupt</tt> exception will happen after the
lock has been acquired.

<P>

<LI>When the main thread exits, it is system defined whether the other
threads survive.  On SGI IRIX using the native thread implementation,
they survive.  On most other systems, they are killed without
executing <tt class=keyword>try</tt> ... <tt class=keyword>finally</tt> clauses or executing
object destructors.
<a name="l2h-1337"></a>

<P>

<LI>When the main thread exits, it does not do any of its usual cleanup
(except that <tt class=keyword>try</tt> ... <tt class=keyword>finally</tt> clauses are honored),
and the standard I/O files are not flushed.

<P>

</UL><p>
<div class=navigation><hr><table align=center width="100%" cellpadding=0 cellspacing=2>
<tr>
<td><A HREF="module-threading.html"><img width=32 height=32 align=bottom border=0 alt="next"
 src="../icons/next.gif"></A></td>
<td><A HREF="someos.html"><img width=32 height=32 align=bottom border=0 alt="up"
 src="../icons/up.gif"></A></td>
<td><A HREF="module-select.html"><img width=32 height=32 align=bottom border=0 alt="previous"
 src="../icons/previous.gif"></A></td>
<td align=center bgcolor="#99CCFF" width="100%">
 <b class=title>Python Library Reference</b></td>
<td><A HREF="contents.html"><img width=32 height=32 align=bottom border=0 alt="contents"
 src="../icons/contents.gif"></A></td>
<td><a
 href="modindex.html"><img width=32 height=32 align=bottom border=0 alt="modules"
 src="../icons/modules.gif"></a></td>
<td><A HREF="genindex.html"><img width=32 height=32 align=bottom border=0 alt="index"
 src="../icons/index.gif"></A></td>
</tr></table><b class=navlabel>Next:</b> <span class=sectref><A HREF="module-threading.html">7.5 threading  </A></span>
<b class=navlabel>Up:</b> <span class=sectref><A HREF="someos.html">7. Optional Operating System</A></span>
<b class=navlabel>Previous:</b> <span class=sectref><A HREF="module-select.html">7.3 select  </A></span>
</div>
<!--End of Navigation Panel-->
<ADDRESS>
<hr>Send comments on this document to <a href="mailto:python-docs@python.org">python-docs@python.org</a>.
</ADDRESS>
</BODY>
</HTML>
