Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / v9 / html / python / lib / module-threading.html
CommitLineData
920dae64
AT
1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2<html>
3<head>
4<link rel="STYLESHEET" href="lib.css" type='text/css' />
5<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
6<link rel='start' href='../index.html' title='Python Documentation Index' />
7<link rel="first" href="lib.html" title='Python Library Reference' />
8<link rel='contents' href='contents.html' title="Contents" />
9<link rel='index' href='genindex.html' title='Index' />
10<link rel='last' href='about.html' title='About this document...' />
11<link rel='help' href='about.html' title='About this document...' />
12<link rel="next" href="module-dummythread.html" />
13<link rel="prev" href="module-thread.html" />
14<link rel="parent" href="someos.html" />
15<link rel="next" href="lock-objects.html" />
16<meta name='aesop' content='information' />
17<title>7.5 threading -- Higher-level threading interface</title>
18</head>
19<body>
20<DIV CLASS="navigation">
21<div id='top-navigation-panel' xml:id='top-navigation-panel'>
22<table align="center" width="100%" cellpadding="0" cellspacing="2">
23<tr>
24<td class='online-navigation'><a rel="prev" title="7.4 thread "
25 href="module-thread.html"><img src='../icons/previous.png'
26 border='0' height='32' alt='Previous Page' width='32' /></A></td>
27<td class='online-navigation'><a rel="parent" title="7. Optional Operating System"
28 href="someos.html"><img src='../icons/up.png'
29 border='0' height='32' alt='Up One Level' width='32' /></A></td>
30<td class='online-navigation'><a rel="next" title="7.5.1 Lock Objects"
31 href="lock-objects.html"><img src='../icons/next.png'
32 border='0' height='32' alt='Next Page' width='32' /></A></td>
33<td align="center" width="100%">Python Library Reference</td>
34<td class='online-navigation'><a rel="contents" title="Table of Contents"
35 href="contents.html"><img src='../icons/contents.png'
36 border='0' height='32' alt='Contents' width='32' /></A></td>
37<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
38 border='0' height='32' alt='Module Index' width='32' /></a></td>
39<td class='online-navigation'><a rel="index" title="Index"
40 href="genindex.html"><img src='../icons/index.png'
41 border='0' height='32' alt='Index' width='32' /></A></td>
42</tr></table>
43<div class='online-navigation'>
44<b class="navlabel">Previous:</b>
45<a class="sectref" rel="prev" href="module-thread.html">7.4 thread </A>
46<b class="navlabel">Up:</b>
47<a class="sectref" rel="parent" href="someos.html">7. Optional Operating System</A>
48<b class="navlabel">Next:</b>
49<a class="sectref" rel="next" href="lock-objects.html">7.5.1 Lock Objects</A>
50</div>
51<hr /></div>
52</DIV>
53<!--End of Navigation Panel-->
54
55<H1><A NAME="SECTION009500000000000000000">
567.5 <tt class="module">threading</tt> --
57 Higher-level threading interface</A>
58</H1>
59
60<P>
61<A NAME="module-threading"></A>
62
63<P>
64This module constructs higher-level threading interfaces on top of the
65lower level <tt class="module"><a href="module-thread.html">thread</a></tt> module.
66
67<P>
68The <tt class="module"><a href="module-dummythreading.html">dummy_threading</a></tt> module is provided for
69situations where <tt class="module">threading</tt> cannot be used because
70<tt class="module"><a href="module-thread.html">thread</a></tt> is missing.
71
72<P>
73This module defines the following functions and objects:
74
75<P>
76<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
77 <td><nobr><b><tt id='l2h-2704' xml:id='l2h-2704' class="function">activeCount</tt></b>(</nobr></td>
78 <td><var></var>)</td></tr></table></dt>
79<dd>
80Return the number of currently active <tt class="class">Thread</tt> objects.
81The returned count is equal to the length of the list returned by
82<tt class="function">enumerate()</tt>.
83A function that returns the number of currently active threads.
84</dl>
85
86<P>
87<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
88 <td><nobr><b><tt id='l2h-2705' xml:id='l2h-2705' class="function">Condition</tt></b>(</nobr></td>
89 <td><var></var>)</td></tr></table></dt>
90<dd>
91A factory function that returns a new condition variable object.
92A condition variable allows one or more threads to wait until they
93are notified by another thread.
94</dl>
95
96<P>
97<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
98 <td><nobr><b><tt id='l2h-2706' xml:id='l2h-2706' class="function">currentThread</tt></b>(</nobr></td>
99 <td><var></var>)</td></tr></table></dt>
100<dd>
101Return the current <tt class="class">Thread</tt> object, corresponding to the
102caller's thread of control. If the caller's thread of control was not
103created through the
104<tt class="module">threading</tt> module, a dummy thread object with limited functionality
105is returned.
106</dl>
107
108<P>
109<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
110 <td><nobr><b><tt id='l2h-2707' xml:id='l2h-2707' class="function">enumerate</tt></b>(</nobr></td>
111 <td><var></var>)</td></tr></table></dt>
112<dd>
113Return a list of all currently active <tt class="class">Thread</tt> objects.
114The list includes daemonic threads, dummy thread objects created
115by <tt class="function">currentThread()</tt>, and the main thread. It excludes terminated
116threads and threads that have not yet been started.
117</dl>
118
119<P>
120<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
121 <td><nobr><b><tt id='l2h-2708' xml:id='l2h-2708' class="function">Event</tt></b>(</nobr></td>
122 <td><var></var>)</td></tr></table></dt>
123<dd>
124A factory function that returns a new event object. An event manages
125a flag that can be set to true with the <tt class="method">set()</tt> method and
126reset to false with the <tt class="method">clear()</tt> method. The <tt class="method">wait()</tt>
127method blocks until the flag is true.
128</dl>
129
130<P>
131<dl><dt><b><span class="typelabel">class</span>&nbsp;<tt id='l2h-2709' xml:id='l2h-2709' class="class">local</tt></b>
132<dd>
133A class that represents thread-local data. Thread-local data are data
134whose values are thread specific. To manage thread-local data, just
135create an instance of <tt class="class">local</tt> (or a subclass) and store
136attributes on it:
137
138<P>
139<div class="verbatim"><pre>
140mydata = threading.local()
141mydata.x = 1
142</pre></div>
143
144<P>
145The instance's values will be different for separate threads.
146
147<P>
148For more details and extensive examples, see the documentation string
149of the <tt class="module">_threading_local</tt> module.
150
151<P>
152
153<span class="versionnote">New in version 2.4.</span>
154
155</dl>
156
157<P>
158<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
159 <td><nobr><b><tt id='l2h-2710' xml:id='l2h-2710' class="function">Lock</tt></b>(</nobr></td>
160 <td><var></var>)</td></tr></table></dt>
161<dd>
162A factory function that returns a new primitive lock object. Once
163a thread has acquired it, subsequent attempts to acquire it block,
164until it is released; any thread may release it.
165</dl>
166
167<P>
168<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
169 <td><nobr><b><tt id='l2h-2711' xml:id='l2h-2711' class="function">RLock</tt></b>(</nobr></td>
170 <td><var></var>)</td></tr></table></dt>
171<dd>
172A factory function that returns a new reentrant lock object.
173A reentrant lock must be released by the thread that acquired it.
174Once a thread has acquired a reentrant lock, the same thread may
175acquire it again without blocking; the thread must release it once
176for each time it has acquired it.
177</dl>
178
179<P>
180<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
181 <td><nobr><b><tt id='l2h-2712' xml:id='l2h-2712' class="function">Semaphore</tt></b>(</nobr></td>
182 <td><var></var><big>[</big><var>value</var><big>]</big><var></var>)</td></tr></table></dt>
183<dd>
184A factory function that returns a new semaphore object. A
185semaphore manages a counter representing the number of <tt class="method">release()</tt>
186calls minus the number of <tt class="method">acquire()</tt> calls, plus an initial value.
187The <tt class="method">acquire()</tt> method blocks if necessary until it can return
188without making the counter negative. If not given, <var>value</var> defaults to
1891.
190</dl>
191
192<P>
193<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
194 <td><nobr><b><tt id='l2h-2713' xml:id='l2h-2713' class="function">BoundedSemaphore</tt></b>(</nobr></td>
195 <td><var></var><big>[</big><var>value</var><big>]</big><var></var>)</td></tr></table></dt>
196<dd>
197A factory function that returns a new bounded semaphore object. A bounded
198semaphore checks to make sure its current value doesn't exceed its initial
199value. If it does, <tt class="exception">ValueError</tt> is raised. In most situations
200semaphores are used to guard resources with limited capacity. If the
201semaphore is released too many times it's a sign of a bug. If not given,
202<var>value</var> defaults to 1.
203</dl>
204
205<P>
206<dl><dt><b><span class="typelabel">class</span>&nbsp;<tt id='l2h-2714' xml:id='l2h-2714' class="class">Thread</tt></b>
207<dd>
208A class that represents a thread of control. This class can be safely
209subclassed in a limited fashion.
210</dl>
211
212<P>
213<dl><dt><b><span class="typelabel">class</span>&nbsp;<tt id='l2h-2715' xml:id='l2h-2715' class="class">Timer</tt></b>
214<dd>
215A thread that executes a function after a specified interval has passed.
216</dl>
217
218<P>
219<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
220 <td><nobr><b><tt id='l2h-2716' xml:id='l2h-2716' class="function">settrace</tt></b>(</nobr></td>
221 <td><var>func</var>)</td></tr></table></dt>
222<dd>
223Set a trace function<a id='l2h-2718' xml:id='l2h-2718'></a> for all threads started
224from the <tt class="module">threading</tt> module. The <var>func</var> will be passed to
225<tt class="function">sys.settrace()</tt> for each thread, before its <tt class="method">run()</tt>
226method is called.
227
228<span class="versionnote">New in version 2.3.</span>
229
230</dl>
231
232<P>
233<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
234 <td><nobr><b><tt id='l2h-2717' xml:id='l2h-2717' class="function">setprofile</tt></b>(</nobr></td>
235 <td><var>func</var>)</td></tr></table></dt>
236<dd>
237Set a profile function<a id='l2h-2719' xml:id='l2h-2719'></a> for all threads started
238from the <tt class="module">threading</tt> module. The <var>func</var> will be passed to
239<tt class="function">sys.setprofile()</tt> for each thread, before its <tt class="method">run()</tt>
240method is called.
241
242<span class="versionnote">New in version 2.3.</span>
243
244</dl>
245
246<P>
247Detailed interfaces for the objects are documented below.
248
249<P>
250The design of this module is loosely based on Java's threading model.
251However, where Java makes locks and condition variables basic behavior
252of every object, they are separate objects in Python. Python's <tt class="class">Thread</tt>
253class supports a subset of the behavior of Java's Thread class;
254currently, there are no priorities, no thread groups, and threads
255cannot be destroyed, stopped, suspended, resumed, or interrupted. The
256static methods of Java's Thread class, when implemented, are mapped to
257module-level functions.
258
259<P>
260All of the methods described below are executed atomically.
261
262<P>
263
264<p><br /></p><hr class='online-navigation' />
265<div class='online-navigation'>
266<!--Table of Child-Links-->
267<A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></a>
268
269<UL CLASS="ChildLinks">
270<LI><A href="lock-objects.html">7.5.1 Lock Objects</a>
271<LI><A href="rlock-objects.html">7.5.2 RLock Objects</a>
272<LI><A href="condition-objects.html">7.5.3 Condition Objects</a>
273<LI><A href="semaphore-objects.html">7.5.4 Semaphore Objects</a>
274<UL>
275<LI><A href="semaphore-examples.html">7.5.4.1 <tt class="class">Semaphore</tt> Example</a>
276</ul>
277<LI><A href="event-objects.html">7.5.5 Event Objects</a>
278<LI><A href="thread-objects.html">7.5.6 Thread Objects</a>
279<LI><A href="timer-objects.html">7.5.7 Timer Objects</a>
280</ul>
281<!--End of Table of Child-Links-->
282</div>
283
284<DIV CLASS="navigation">
285<div class='online-navigation'>
286<p></p><hr />
287<table align="center" width="100%" cellpadding="0" cellspacing="2">
288<tr>
289<td class='online-navigation'><a rel="prev" title="7.4 thread "
290 href="module-thread.html"><img src='../icons/previous.png'
291 border='0' height='32' alt='Previous Page' width='32' /></A></td>
292<td class='online-navigation'><a rel="parent" title="7. Optional Operating System"
293 href="someos.html"><img src='../icons/up.png'
294 border='0' height='32' alt='Up One Level' width='32' /></A></td>
295<td class='online-navigation'><a rel="next" title="7.5.1 Lock Objects"
296 href="lock-objects.html"><img src='../icons/next.png'
297 border='0' height='32' alt='Next Page' width='32' /></A></td>
298<td align="center" width="100%">Python Library Reference</td>
299<td class='online-navigation'><a rel="contents" title="Table of Contents"
300 href="contents.html"><img src='../icons/contents.png'
301 border='0' height='32' alt='Contents' width='32' /></A></td>
302<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
303 border='0' height='32' alt='Module Index' width='32' /></a></td>
304<td class='online-navigation'><a rel="index" title="Index"
305 href="genindex.html"><img src='../icons/index.png'
306 border='0' height='32' alt='Index' width='32' /></A></td>
307</tr></table>
308<div class='online-navigation'>
309<b class="navlabel">Previous:</b>
310<a class="sectref" rel="prev" href="module-thread.html">7.4 thread </A>
311<b class="navlabel">Up:</b>
312<a class="sectref" rel="parent" href="someos.html">7. Optional Operating System</A>
313<b class="navlabel">Next:</b>
314<a class="sectref" rel="next" href="lock-objects.html">7.5.1 Lock Objects</A>
315</div>
316</div>
317<hr />
318<span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span>
319</DIV>
320<!--End of Navigation Panel-->
321<ADDRESS>
322See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
323</ADDRESS>
324</BODY>
325</HTML>