blob: e896a84d8112f8cfe0965936bc716d0fc7db4762 [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{threading} ---
Fred Drakebf5a6d21999-03-12 19:57:38 +00002 Higher-level threading interface}
Fred Drakeb91e9341998-07-23 17:59:49 +00003
Fred Drakebf5a6d21999-03-12 19:57:38 +00004\declaremodule{standard}{threading}
5\modulesynopsis{Higher-level threading interface.}
Fred Drakeb91e9341998-07-23 17:59:49 +00006
Andrew M. Kuchling16440e61998-07-20 13:46:10 +00007
Fred Drake9643c671998-07-27 22:06:12 +00008This module constructs higher-level threading interfaces on top of the
Fred Drakeffbe6871999-04-22 21:23:22 +00009lower level \refmodule{thread} module.
Andrew M. Kuchling16440e61998-07-20 13:46:10 +000010
Fred Drake9643c671998-07-27 22:06:12 +000011This module is safe for use with \samp{from threading import *}. It
Andrew M. Kuchling16440e61998-07-20 13:46:10 +000012defines the following functions and objects:
13
14\begin{funcdesc}{activeCount}{}
15Return the number of currently active \class{Thread} objects.
16The returned count is equal to the length of the list returned by
17\function{enumerate()}.
18A function that returns the number of currently active threads.
19\end{funcdesc}
20
21\begin{funcdesc}{Condition}{}
22A factory function that returns a new condition variable object.
23A condition variable allows one or more threads to wait until they
24are notified by another thread.
25\end{funcdesc}
26
27\begin{funcdesc}{currentThread}{}
28Return the current \class{Thread} object, corresponding to the
29caller's thread of control. If the caller's thread of control was not
30created through the
31\module{threading} module, a dummy thread object with limited functionality
32is returned.
33\end{funcdesc}
34
35\begin{funcdesc}{enumerate}{}
36Return a list of all currently active \class{Thread} objects.
37The list includes daemonic threads, dummy thread objects created
38by \function{currentThread()}, and the main thread. It excludes terminated
39threads and threads that have not yet been started.
40\end{funcdesc}
41
42\begin{funcdesc}{Event}{}
43A factory function that returns a new event object. An event
44manages a flag that can be set to true with the \method{set()} method and
45reset to false with the \method{clear()} method. The \method{wait()} method blocks
46until the flag is true.
47\end{funcdesc}
48
49\begin{funcdesc}{Lock}{}
50A factory function that returns a new primitive lock object. Once
51a thread has acquired it, subsequent attempts to acquire it block,
52until it is released; any thread may release it.
53\end{funcdesc}
54
55\begin{funcdesc}{RLock}{}
56A factory function that returns a new reentrant lock object.
57A reentrant lock must be released by the thread that acquired it.
58Once a thread has acquired a reentrant lock, the same thread may
59acquire it again without blocking; the thread must release it once
60for each time it has acquired it.
61\end{funcdesc}
62
63\begin{funcdesc}{Semaphore}{}
64A factory function that returns a new semaphore object. A
65semaphore manages a counter representing the number of \method{release()}
66calls minus the number of \method{acquire()} calls, plus an initial value.
67The \method{acquire()} method blocks if necessary until it can return
68without making the counter negative.
69\end{funcdesc}
70
Fred Drakec19f3922001-05-31 20:24:07 +000071\begin{classdesc*}{Thread}{}
Andrew M. Kuchling16440e61998-07-20 13:46:10 +000072A class that represents a thread of control. This class can be safely subclassed in a limited fashion.
Fred Drakec19f3922001-05-31 20:24:07 +000073\end{classdesc*}
Andrew M. Kuchling16440e61998-07-20 13:46:10 +000074
75Detailed interfaces for the objects are documented below.
76
77The design of this module is loosely based on Java's threading model.
78However, where Java makes locks and condition variables basic behavior
79of every object, they are separate objects in Python. Python's \class{Thread}
80class supports a subset of the behavior of Java's Thread class;
81currently, there are no priorities, no thread groups, and threads
82cannot be destroyed, stopped, suspended, resumed, or interrupted. The
83static methods of Java's Thread class, when implemented, are mapped to
84module-level functions.
85
86All of the methods described below are executed atomically.
87
Fred Drakebf5a6d21999-03-12 19:57:38 +000088
89\subsection{Lock Objects \label{lock-objects}}
Andrew M. Kuchling16440e61998-07-20 13:46:10 +000090
91A primitive lock is a synchronization primitive that is not owned
92by a particular thread when locked. In Python, it is currently
93the lowest level synchronization primitive available, implemented
Fred Drakeffbe6871999-04-22 21:23:22 +000094directly by the \refmodule{thread} extension module.
Andrew M. Kuchling16440e61998-07-20 13:46:10 +000095
96A primitive lock is in one of two states, ``locked'' or ``unlocked''.
97It is created in the unlocked state. It has two basic methods,
98\method{acquire()} and \method{release()}. When the state is
99unlocked, \method{acquire()} changes the state to locked and returns
100immediately. When the state is locked, \method{acquire()} blocks
101until a call to \method{release()} in another thread changes it to
102unlocked, then the \method{acquire()} call resets it to locked and
103returns. The \method{release()} method should only be called in the
104locked state; it changes the state to unlocked and returns
105immediately. When more than one thread is blocked in
106\method{acquire()} waiting for the state to turn to unlocked, only one
107thread proceeds when a \method{release()} call resets the state to
108unlocked; which one of the waiting threads proceeds is not defined,
109and may vary across implementations.
110
111All methods are executed atomically.
112
Fred Drakebf5a6d21999-03-12 19:57:38 +0000113\begin{methoddesc}{acquire}{\optional{blocking\code{ = 1}}}
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000114Acquire a lock, blocking or non-blocking.
115
116When invoked without arguments, block until the lock is
117unlocked, then set it to locked, and return. There is no
118return value in this case.
119
120When invoked with the \var{blocking} argument set to true, do the
121same thing as when called without arguments, and return true.
122
123When invoked with the \var{blocking} argument set to false, do not
124block. If a call without an argument would block, return false
125immediately; otherwise, do the same thing as when called
126without arguments, and return true.
127\end{methoddesc}
128
129\begin{methoddesc}{release}{}
130Release a lock.
131
132When the lock is locked, reset it to unlocked, and return. If
133any other threads are blocked waiting for the lock to become
134unlocked, allow exactly one of them to proceed.
135
136Do not call this method when the lock is unlocked.
137
138There is no return value.
139\end{methoddesc}
140
Fred Drakebf5a6d21999-03-12 19:57:38 +0000141
142\subsection{RLock Objects \label{rlock-objects}}
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000143
144A reentrant lock is a synchronization primitive that may be
145acquired multiple times by the same thread. Internally, it uses
146the concepts of ``owning thread'' and ``recursion level'' in
147addition to the locked/unlocked state used by primitive locks. In
148the locked state, some thread owns the lock; in the unlocked
149state, no thread owns it.
150
151To lock the lock, a thread calls its \method{acquire()} method; this
152returns once the thread owns the lock. To unlock the lock, a
Fred Drake907e76b2001-07-06 20:30:11 +0000153thread calls its \method{release()} method.
154\method{acquire()}/\method{release()} call pairs may be nested; only
155the final \method{release()} (the \method{release()} of the outermost
156pair) resets the lock to unlocked and allows another thread blocked in
157\method{acquire()} to proceed.
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000158
Fred Drakebf5a6d21999-03-12 19:57:38 +0000159\begin{methoddesc}{acquire}{\optional{blocking\code{ = 1}}}
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000160Acquire a lock, blocking or non-blocking.
161
162When invoked without arguments: if this thread already owns
163the lock, increment the recursion level by one, and return
164immediately. Otherwise, if another thread owns the lock,
165block until the lock is unlocked. Once the lock is unlocked
166(not owned by any thread), then grab ownership, set the
167recursion level to one, and return. If more than one thread
168is blocked waiting until the lock is unlocked, only one at a
169time will be able to grab ownership of the lock. There is no
170return value in this case.
171
172When invoked with the \var{blocking} argument set to true, do the
173same thing as when called without arguments, and return true.
174
175When invoked with the \var{blocking} argument set to false, do not
176block. If a call without an argument would block, return false
177immediately; otherwise, do the same thing as when called
178without arguments, and return true.
179\end{methoddesc}
180
181\begin{methoddesc}{release}{}
182Release a lock, decrementing the recursion level. If after the
183decrement it is zero, reset the lock to unlocked (not owned by any
184thread), and if any other threads are blocked waiting for the lock to
185become unlocked, allow exactly one of them to proceed. If after the
186decrement the recursion level is still nonzero, the lock remains
187locked and owned by the calling thread.
188
189Only call this method when the calling thread owns the lock.
190Do not call this method when the lock is unlocked.
191
192There is no return value.
193\end{methoddesc}
194
Fred Drakebf5a6d21999-03-12 19:57:38 +0000195
196\subsection{Condition Objects \label{condition-objects}}
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000197
198A condition variable is always associated with some kind of lock;
199this can be passed in or one will be created by default. (Passing
200one in is useful when several condition variables must share the
201same lock.)
202
203A condition variable has \method{acquire()} and \method{release()}
204methods that call the corresponding methods of the associated lock.
205It also has a \method{wait()} method, and \method{notify()} and
206\method{notifyAll()} methods. These three must only be called when
207the calling thread has acquired the lock.
208
209The \method{wait()} method releases the lock, and then blocks until it
210is awakened by a \method{notify()} or \method{notifyAll()} call for
211the same condition variable in another thread. Once awakened, it
212re-acquires the lock and returns. It is also possible to specify a
213timeout.
214
215The \method{notify()} method wakes up one of the threads waiting for
216the condition variable, if any are waiting. The \method{notifyAll()}
217method wakes up all threads waiting for the condition variable.
218
219Note: the \method{notify()} and \method{notifyAll()} methods don't
220release the lock; this means that the thread or threads awakened will
221not return from their \method{wait()} call immediately, but only when
222the thread that called \method{notify()} or \method{notifyAll()}
223finally relinquishes ownership of the lock.
224
225Tip: the typical programming style using condition variables uses the
226lock to synchronize access to some shared state; threads that are
227interested in a particular change of state call \method{wait()}
228repeatedly until they see the desired state, while threads that modify
229the state call \method{notify()} or \method{notifyAll()} when they
230change the state in such a way that it could possibly be a desired
231state for one of the waiters. For example, the following code is a
232generic producer-consumer situation with unlimited buffer capacity:
233
234\begin{verbatim}
235# Consume one item
236cv.acquire()
237while not an_item_is_available():
238 cv.wait()
239get_an_available_item()
240cv.release()
241
242# Produce one item
243cv.acquire()
244make_an_item_available()
245cv.notify()
246cv.release()
247\end{verbatim}
248
249To choose between \method{notify()} and \method{notifyAll()}, consider
250whether one state change can be interesting for only one or several
251waiting threads. E.g. in a typical producer-consumer situation,
252adding one item to the buffer only needs to wake up one consumer
253thread.
254
Fred Drakebf5a6d21999-03-12 19:57:38 +0000255\begin{classdesc}{Condition}{\optional{lock}}
256If the \var{lock} argument is given and not \code{None}, it must be a
257\class{Lock} or \class{RLock} object, and it is used as the underlying
258lock. Otherwise, a new \class{RLock} object is created and used as
259the underlying lock.
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000260\end{classdesc}
261
262\begin{methoddesc}{acquire}{*args}
263Acquire the underlying lock.
264This method calls the corresponding method on the underlying
265lock; the return value is whatever that method returns.
266\end{methoddesc}
267
268\begin{methoddesc}{release}{}
269Release the underlying lock.
270This method calls the corresponding method on the underlying
271lock; there is no return value.
272\end{methoddesc}
273
Fred Drakebf5a6d21999-03-12 19:57:38 +0000274\begin{methoddesc}{wait}{\optional{timeout}}
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000275Wait until notified or until a timeout occurs.
276This must only be called when the calling thread has acquired the
277lock.
278
279This method releases the underlying lock, and then blocks until it is
280awakened by a \method{notify()} or \method{notifyAll()} call for the
281same condition variable in another thread, or until the optional
282timeout occurs. Once awakened or timed out, it re-acquires the lock
283and returns.
284
Fred Drakebf5a6d21999-03-12 19:57:38 +0000285When the \var{timeout} argument is present and not \code{None}, it
286should be a floating point number specifying a timeout for the
287operation in seconds (or fractions thereof).
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000288
Fred Drakebf5a6d21999-03-12 19:57:38 +0000289When the underlying lock is an \class{RLock}, it is not released using
290its \method{release()} method, since this may not actually unlock the
291lock when it was acquired multiple times recursively. Instead, an
292internal interface of the \class{RLock} class is used, which really
293unlocks it even when it has been recursively acquired several times.
294Another internal interface is then used to restore the recursion level
295when the lock is reacquired.
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000296\end{methoddesc}
297
298\begin{methoddesc}{notify}{}
299Wake up a thread waiting on this condition, if any.
300This must only be called when the calling thread has acquired the
301lock.
302
303This method wakes up one of the threads waiting for the condition
304variable, if any are waiting; it is a no-op if no threads are waiting.
305
306The current implementation wakes up exactly one thread, if any are
307waiting. However, it's not safe to rely on this behavior. A future,
308optimized implementation may occasionally wake up more than one
309thread.
310
311Note: the awakened thread does not actually return from its
312\method{wait()} call until it can reacquire the lock. Since
313\method{notify()} does not release the lock, its caller should.
314\end{methoddesc}
315
316\begin{methoddesc}{notifyAll}{}
317Wake up all threads waiting on this condition. This method acts like
318\method{notify()}, but wakes up all waiting threads instead of one.
319\end{methoddesc}
320
Fred Drakebf5a6d21999-03-12 19:57:38 +0000321
322\subsection{Semaphore Objects \label{semaphore-objects}}
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000323
324This is one of the oldest synchronization primitives in the history of
325computer science, invented by the early Dutch computer scientist
Fred Drakebf5a6d21999-03-12 19:57:38 +0000326Edsger W. Dijkstra (he used \method{P()} and \method{V()} instead of
327\method{acquire()} and \method{release()}).
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000328
329A semaphore manages an internal counter which is decremented by each
330\method{acquire()} call and incremented by each \method{release()}
331call. The counter can never go below zero; when \method{acquire()}
332finds that it is zero, it blocks, waiting until some other thread
333calls \method{release()}.
334
Fred Drakebf5a6d21999-03-12 19:57:38 +0000335\begin{classdesc}{Semaphore}{\optional{value}}
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000336The optional argument gives the initial value for the internal
Fred Drakebf5a6d21999-03-12 19:57:38 +0000337counter; it defaults to \code{1}.
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000338\end{classdesc}
339
Fred Drakebf5a6d21999-03-12 19:57:38 +0000340\begin{methoddesc}{acquire}{\optional{blocking}}
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000341Acquire a semaphore.
342
343When invoked without arguments: if the internal counter is larger than
344zero on entry, decrement it by one and return immediately. If it is
345zero on entry, block, waiting until some other thread has called
346\method{release()} to make it larger than zero. This is done with
347proper interlocking so that if multiple \method{acquire()} calls are
348blocked, \method{release()} will wake exactly one of them up. The
349implementation may pick one at random, so the order in which blocked
350threads are awakened should not be relied on. There is no return
351value in this case.
352
Fred Drakebf5a6d21999-03-12 19:57:38 +0000353When invoked with \var{blocking} set to true, do the same thing as
354when called without arguments, and return true.
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000355
Fred Drakebf5a6d21999-03-12 19:57:38 +0000356When invoked with \var{blocking} set to false, do not block. If a
357call without an argument would block, return false immediately;
358otherwise, do the same thing as when called without arguments, and
359return true.
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000360\end{methoddesc}
361
362\begin{methoddesc}{release}{}
363Release a semaphore,
364incrementing the internal counter by one. When it was zero on
365entry and another thread is waiting for it to become larger
366than zero again, wake up that thread.
367\end{methoddesc}
368
Fred Drakebf5a6d21999-03-12 19:57:38 +0000369
370\subsection{Event Objects \label{event-objects}}
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000371
372This is one of the simplest mechanisms for communication between
Fred Drake86119211999-04-23 20:07:02 +0000373threads: one thread signals an event and one or more other threads
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000374are waiting for it.
375
376An event object manages an internal flag that can be set to true with
377the \method{set()} method and reset to false with the \method{clear()} method. The
378\method{wait()} method blocks until the flag is true.
379
380
381\begin{classdesc}{Event}{}
382The internal flag is initially false.
383\end{classdesc}
384
385\begin{methoddesc}{isSet}{}
386Return true if and only if the internal flag is true.
387\end{methoddesc}
388
389\begin{methoddesc}{set}{}
390Set the internal flag to true.
391All threads waiting for it to become true are awakened.
392Threads that call \method{wait()} once the flag is true will not block
393at all.
394\end{methoddesc}
395
396\begin{methoddesc}{clear}{}
397Reset the internal flag to false.
398Subsequently, threads calling \method{wait()} will block until \method{set()} is
399called to set the internal flag to true again.
400\end{methoddesc}
401
Fred Drakebf5a6d21999-03-12 19:57:38 +0000402\begin{methoddesc}{wait}{\optional{timeout}}
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000403Block until the internal flag is true.
404If the internal flag is true on entry, return immediately. Otherwise,
405block until another thread calls \method{set()} to set the flag to
406true, or until the optional timeout occurs.
407
408When the timeout argument is present and not \code{None}, it should be a
409floating point number specifying a timeout for the operation in
410seconds (or fractions thereof).
411\end{methoddesc}
412
Fred Drakebf5a6d21999-03-12 19:57:38 +0000413
414\subsection{Thread Objects \label{thread-objects}}
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000415
416This class represents an activity that is run in a separate thread
417of control. There are two ways to specify the activity: by
418passing a callable object to the constructor, or by overriding the
419\method{run()} method in a subclass. No other methods (except for the
420constructor) should be overridden in a subclass. In other words,
Fred Drakebf5a6d21999-03-12 19:57:38 +0000421\emph{only} override the \method{__init__()} and \method{run()}
422methods of this class.
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000423
424Once a thread object is created, its activity must be started by
Fred Drakebf5a6d21999-03-12 19:57:38 +0000425calling the thread's \method{start()} method. This invokes the
426\method{run()} method in a separate thread of control.
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000427
428Once the thread's activity is started, the thread is considered
429'alive' and 'active' (these concepts are almost, but not quite
430exactly, the same; their definition is intentionally somewhat
Fred Drakebf5a6d21999-03-12 19:57:38 +0000431vague). It stops being alive and active when its \method{run()}
432method terminates -- either normally, or by raising an unhandled
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000433exception. The \method{isAlive()} method tests whether the thread is
434alive.
435
Fred Drakebf5a6d21999-03-12 19:57:38 +0000436Other threads can call a thread's \method{join()} method. This blocks
437the calling thread until the thread whose \method{join()} method is
438called is terminated.
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000439
440A thread has a name. The name can be passed to the constructor,
Fred Drakebf5a6d21999-03-12 19:57:38 +0000441set with the \method{setName()} method, and retrieved with the
442\method{getName()} method.
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000443
444A thread can be flagged as a ``daemon thread''. The significance
445of this flag is that the entire Python program exits when only
446daemon threads are left. The initial value is inherited from the
Fred Drakebf5a6d21999-03-12 19:57:38 +0000447creating thread. The flag can be set with the \method{setDaemon()}
Fred Drakec19f3922001-05-31 20:24:07 +0000448method and retrieved with the \method{isDaemon()} method.
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000449
450There is a ``main thread'' object; this corresponds to the
451initial thread of control in the Python program. It is not a
452daemon thread.
453
454There is the possibility that ``dummy thread objects'' are
455created. These are thread objects corresponding to ``alien
456threads''. These are threads of control started outside the
Fred Drake907e76b2001-07-06 20:30:11 +0000457threading module, such as directly from C code. Dummy thread objects
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000458have limited functionality; they are always considered alive,
Fred Drakebf5a6d21999-03-12 19:57:38 +0000459active, and daemonic, and cannot be \method{join()}ed. They are never
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000460deleted, since it is impossible to detect the termination of alien
461threads.
462
463
464\begin{classdesc}{Thread}{group=None, target=None, name=None,
Fred Drakebf5a6d21999-03-12 19:57:38 +0000465 args=(), kwargs=\{\}}
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000466This constructor should always be called with keyword
467arguments. Arguments are:
468
Fred Drakec19f3922001-05-31 20:24:07 +0000469\var{group} should be \code{None}; reserved for future extension when
470a \class{ThreadGroup} class is implemented.
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000471
Fred Drakec19f3922001-05-31 20:24:07 +0000472\var{target} is the callable object to be invoked by the
473\method{run()} method. Defaults to \code{None}, meaning nothing is
474called.
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000475
Fred Drakec19f3922001-05-31 20:24:07 +0000476\var{name} is the thread name. By default, a unique name is
477constructed of the form ``Thread-\var{N}'' where \var{N} is a small
478decimal number.
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000479
Fred Drakec19f3922001-05-31 20:24:07 +0000480\var{args} is the argument tuple for the target invocation. Defaults
481to \code{()}.
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000482
Fred Drakec19f3922001-05-31 20:24:07 +0000483\var{kwargs} is a dictionary of keyword arguments for the target
484invocation. Defaults to \code{\{\}}.
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000485
486If the subclass overrides the constructor, it must make sure
Fred Drakebf5a6d21999-03-12 19:57:38 +0000487to invoke the base class constructor (\code{Thread.__init__()})
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000488before doing anything else to the thread.
489\end{classdesc}
490
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000491\begin{methoddesc}{start}{}
492Start the thread's activity.
493
494This must be called at most once per thread object. It
495arranges for the object's \method{run()} method to be invoked in a
496separate thread of control.
497\end{methoddesc}
498
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000499\begin{methoddesc}{run}{}
500Method representing the thread's activity.
501
502You may override this method in a subclass. The standard
503\method{run()} method invokes the callable object passed to the object's constructor as the
504\var{target} argument, if any, with sequential and keyword
505arguments taken from the \var{args} and \var{kwargs} arguments,
506respectively.
507\end{methoddesc}
508
Fred Drakebf5a6d21999-03-12 19:57:38 +0000509\begin{methoddesc}{join}{\optional{timeout}}
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000510Wait until the thread terminates.
511This blocks the calling thread until the thread whose \method{join()}
512method is called terminates -- either normally or through an
513unhandled exception -- or until the optional timeout occurs.
514
515When the \var{timeout} argument is present and not \code{None}, it should
516be a floating point number specifying a timeout for the
517operation in seconds (or fractions thereof).
518
519A thread can be \method{join()}ed many times.
520
521A thread cannot join itself because this would cause a
522deadlock.
523
524It is an error to attempt to \method{join()} a thread before it has
525been started.
526\end{methoddesc}
527
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000528\begin{methoddesc}{getName}{}
529Return the thread's name.
530\end{methoddesc}
531
532\begin{methoddesc}{setName}{name}
533Set the thread's name.
534
535The name is a string used for identification purposes only.
536It has no semantics. Multiple threads may be given the same
537name. The initial name is set by the constructor.
538\end{methoddesc}
539
540\begin{methoddesc}{isAlive}{}
541Return whether the thread is alive.
542
543Roughly, a thread is alive from the moment the \method{start()} method
544returns until its \method{run()} method terminates.
545\end{methoddesc}
546
547\begin{methoddesc}{isDaemon}{}
548Return the thread's daemon flag.
549\end{methoddesc}
550
551\begin{methoddesc}{setDaemon}{daemonic}
552Set the thread's daemon flag to the Boolean value \var{daemonic}.
553This must be called before \method{start()} is called.
554
555The initial value is inherited from the creating thread.
556
557The entire Python program exits when no active non-daemon
558threads are left.
559\end{methoddesc}