blob: eb0985f0b631fa58ec52ca13feb3d43366d1db97 [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
71\begin{classdesc}{Thread}{}
72A class that represents a thread of control. This class can be safely subclassed in a limited fashion.
73\end{classdesc}
74
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
153thread calls its \method{release()} method. \method{acquire()}/\method{release()} call pairs
154may be nested; only the final \method{release()} (i.e. the \method{release()} of the
155outermost pair) resets the lock to unlocked and allows another
156thread blocked in \method{acquire()} to proceed.
157
Fred Drakebf5a6d21999-03-12 19:57:38 +0000158\begin{methoddesc}{acquire}{\optional{blocking\code{ = 1}}}
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000159Acquire a lock, blocking or non-blocking.
160
161When invoked without arguments: if this thread already owns
162the lock, increment the recursion level by one, and return
163immediately. Otherwise, if another thread owns the lock,
164block until the lock is unlocked. Once the lock is unlocked
165(not owned by any thread), then grab ownership, set the
166recursion level to one, and return. If more than one thread
167is blocked waiting until the lock is unlocked, only one at a
168time will be able to grab ownership of the lock. There is no
169return value in this case.
170
171When invoked with the \var{blocking} argument set to true, do the
172same thing as when called without arguments, and return true.
173
174When invoked with the \var{blocking} argument set to false, do not
175block. If a call without an argument would block, return false
176immediately; otherwise, do the same thing as when called
177without arguments, and return true.
178\end{methoddesc}
179
180\begin{methoddesc}{release}{}
181Release a lock, decrementing the recursion level. If after the
182decrement it is zero, reset the lock to unlocked (not owned by any
183thread), and if any other threads are blocked waiting for the lock to
184become unlocked, allow exactly one of them to proceed. If after the
185decrement the recursion level is still nonzero, the lock remains
186locked and owned by the calling thread.
187
188Only call this method when the calling thread owns the lock.
189Do not call this method when the lock is unlocked.
190
191There is no return value.
192\end{methoddesc}
193
Fred Drakebf5a6d21999-03-12 19:57:38 +0000194
195\subsection{Condition Objects \label{condition-objects}}
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000196
197A condition variable is always associated with some kind of lock;
198this can be passed in or one will be created by default. (Passing
199one in is useful when several condition variables must share the
200same lock.)
201
202A condition variable has \method{acquire()} and \method{release()}
203methods that call the corresponding methods of the associated lock.
204It also has a \method{wait()} method, and \method{notify()} and
205\method{notifyAll()} methods. These three must only be called when
206the calling thread has acquired the lock.
207
208The \method{wait()} method releases the lock, and then blocks until it
209is awakened by a \method{notify()} or \method{notifyAll()} call for
210the same condition variable in another thread. Once awakened, it
211re-acquires the lock and returns. It is also possible to specify a
212timeout.
213
214The \method{notify()} method wakes up one of the threads waiting for
215the condition variable, if any are waiting. The \method{notifyAll()}
216method wakes up all threads waiting for the condition variable.
217
218Note: the \method{notify()} and \method{notifyAll()} methods don't
219release the lock; this means that the thread or threads awakened will
220not return from their \method{wait()} call immediately, but only when
221the thread that called \method{notify()} or \method{notifyAll()}
222finally relinquishes ownership of the lock.
223
224Tip: the typical programming style using condition variables uses the
225lock to synchronize access to some shared state; threads that are
226interested in a particular change of state call \method{wait()}
227repeatedly until they see the desired state, while threads that modify
228the state call \method{notify()} or \method{notifyAll()} when they
229change the state in such a way that it could possibly be a desired
230state for one of the waiters. For example, the following code is a
231generic producer-consumer situation with unlimited buffer capacity:
232
233\begin{verbatim}
234# Consume one item
235cv.acquire()
236while not an_item_is_available():
237 cv.wait()
238get_an_available_item()
239cv.release()
240
241# Produce one item
242cv.acquire()
243make_an_item_available()
244cv.notify()
245cv.release()
246\end{verbatim}
247
248To choose between \method{notify()} and \method{notifyAll()}, consider
249whether one state change can be interesting for only one or several
250waiting threads. E.g. in a typical producer-consumer situation,
251adding one item to the buffer only needs to wake up one consumer
252thread.
253
Fred Drakebf5a6d21999-03-12 19:57:38 +0000254\begin{classdesc}{Condition}{\optional{lock}}
255If the \var{lock} argument is given and not \code{None}, it must be a
256\class{Lock} or \class{RLock} object, and it is used as the underlying
257lock. Otherwise, a new \class{RLock} object is created and used as
258the underlying lock.
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000259\end{classdesc}
260
261\begin{methoddesc}{acquire}{*args}
262Acquire the underlying lock.
263This method calls the corresponding method on the underlying
264lock; the return value is whatever that method returns.
265\end{methoddesc}
266
267\begin{methoddesc}{release}{}
268Release the underlying lock.
269This method calls the corresponding method on the underlying
270lock; there is no return value.
271\end{methoddesc}
272
Fred Drakebf5a6d21999-03-12 19:57:38 +0000273\begin{methoddesc}{wait}{\optional{timeout}}
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000274Wait until notified or until a timeout occurs.
275This must only be called when the calling thread has acquired the
276lock.
277
278This method releases the underlying lock, and then blocks until it is
279awakened by a \method{notify()} or \method{notifyAll()} call for the
280same condition variable in another thread, or until the optional
281timeout occurs. Once awakened or timed out, it re-acquires the lock
282and returns.
283
Fred Drakebf5a6d21999-03-12 19:57:38 +0000284When the \var{timeout} argument is present and not \code{None}, it
285should be a floating point number specifying a timeout for the
286operation in seconds (or fractions thereof).
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000287
Fred Drakebf5a6d21999-03-12 19:57:38 +0000288When the underlying lock is an \class{RLock}, it is not released using
289its \method{release()} method, since this may not actually unlock the
290lock when it was acquired multiple times recursively. Instead, an
291internal interface of the \class{RLock} class is used, which really
292unlocks it even when it has been recursively acquired several times.
293Another internal interface is then used to restore the recursion level
294when the lock is reacquired.
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000295\end{methoddesc}
296
297\begin{methoddesc}{notify}{}
298Wake up a thread waiting on this condition, if any.
299This must only be called when the calling thread has acquired the
300lock.
301
302This method wakes up one of the threads waiting for the condition
303variable, if any are waiting; it is a no-op if no threads are waiting.
304
305The current implementation wakes up exactly one thread, if any are
306waiting. However, it's not safe to rely on this behavior. A future,
307optimized implementation may occasionally wake up more than one
308thread.
309
310Note: the awakened thread does not actually return from its
311\method{wait()} call until it can reacquire the lock. Since
312\method{notify()} does not release the lock, its caller should.
313\end{methoddesc}
314
315\begin{methoddesc}{notifyAll}{}
316Wake up all threads waiting on this condition. This method acts like
317\method{notify()}, but wakes up all waiting threads instead of one.
318\end{methoddesc}
319
Fred Drakebf5a6d21999-03-12 19:57:38 +0000320
321\subsection{Semaphore Objects \label{semaphore-objects}}
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000322
323This is one of the oldest synchronization primitives in the history of
324computer science, invented by the early Dutch computer scientist
Fred Drakebf5a6d21999-03-12 19:57:38 +0000325Edsger W. Dijkstra (he used \method{P()} and \method{V()} instead of
326\method{acquire()} and \method{release()}).
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000327
328A semaphore manages an internal counter which is decremented by each
329\method{acquire()} call and incremented by each \method{release()}
330call. The counter can never go below zero; when \method{acquire()}
331finds that it is zero, it blocks, waiting until some other thread
332calls \method{release()}.
333
Fred Drakebf5a6d21999-03-12 19:57:38 +0000334\begin{classdesc}{Semaphore}{\optional{value}}
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000335The optional argument gives the initial value for the internal
Fred Drakebf5a6d21999-03-12 19:57:38 +0000336counter; it defaults to \code{1}.
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000337\end{classdesc}
338
Fred Drakebf5a6d21999-03-12 19:57:38 +0000339\begin{methoddesc}{acquire}{\optional{blocking}}
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000340Acquire a semaphore.
341
342When invoked without arguments: if the internal counter is larger than
343zero on entry, decrement it by one and return immediately. If it is
344zero on entry, block, waiting until some other thread has called
345\method{release()} to make it larger than zero. This is done with
346proper interlocking so that if multiple \method{acquire()} calls are
347blocked, \method{release()} will wake exactly one of them up. The
348implementation may pick one at random, so the order in which blocked
349threads are awakened should not be relied on. There is no return
350value in this case.
351
Fred Drakebf5a6d21999-03-12 19:57:38 +0000352When invoked with \var{blocking} set to true, do the same thing as
353when called without arguments, and return true.
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000354
Fred Drakebf5a6d21999-03-12 19:57:38 +0000355When invoked with \var{blocking} set to false, do not block. If a
356call without an argument would block, return false immediately;
357otherwise, do the same thing as when called without arguments, and
358return true.
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000359\end{methoddesc}
360
361\begin{methoddesc}{release}{}
362Release a semaphore,
363incrementing the internal counter by one. When it was zero on
364entry and another thread is waiting for it to become larger
365than zero again, wake up that thread.
366\end{methoddesc}
367
Fred Drakebf5a6d21999-03-12 19:57:38 +0000368
369\subsection{Event Objects \label{event-objects}}
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000370
371This is one of the simplest mechanisms for communication between
Fred Drake86119211999-04-23 20:07:02 +0000372threads: one thread signals an event and one or more other threads
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000373are waiting for it.
374
375An event object manages an internal flag that can be set to true with
376the \method{set()} method and reset to false with the \method{clear()} method. The
377\method{wait()} method blocks until the flag is true.
378
379
380\begin{classdesc}{Event}{}
381The internal flag is initially false.
382\end{classdesc}
383
384\begin{methoddesc}{isSet}{}
385Return true if and only if the internal flag is true.
386\end{methoddesc}
387
388\begin{methoddesc}{set}{}
389Set the internal flag to true.
390All threads waiting for it to become true are awakened.
391Threads that call \method{wait()} once the flag is true will not block
392at all.
393\end{methoddesc}
394
395\begin{methoddesc}{clear}{}
396Reset the internal flag to false.
397Subsequently, threads calling \method{wait()} will block until \method{set()} is
398called to set the internal flag to true again.
399\end{methoddesc}
400
Fred Drakebf5a6d21999-03-12 19:57:38 +0000401\begin{methoddesc}{wait}{\optional{timeout}}
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000402Block until the internal flag is true.
403If the internal flag is true on entry, return immediately. Otherwise,
404block until another thread calls \method{set()} to set the flag to
405true, or until the optional timeout occurs.
406
407When the timeout argument is present and not \code{None}, it should be a
408floating point number specifying a timeout for the operation in
409seconds (or fractions thereof).
410\end{methoddesc}
411
Fred Drakebf5a6d21999-03-12 19:57:38 +0000412
413\subsection{Thread Objects \label{thread-objects}}
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000414
415This class represents an activity that is run in a separate thread
416of control. There are two ways to specify the activity: by
417passing a callable object to the constructor, or by overriding the
418\method{run()} method in a subclass. No other methods (except for the
419constructor) should be overridden in a subclass. In other words,
Fred Drakebf5a6d21999-03-12 19:57:38 +0000420\emph{only} override the \method{__init__()} and \method{run()}
421methods of this class.
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000422
423Once a thread object is created, its activity must be started by
Fred Drakebf5a6d21999-03-12 19:57:38 +0000424calling the thread's \method{start()} method. This invokes the
425\method{run()} method in a separate thread of control.
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000426
427Once the thread's activity is started, the thread is considered
428'alive' and 'active' (these concepts are almost, but not quite
429exactly, the same; their definition is intentionally somewhat
Fred Drakebf5a6d21999-03-12 19:57:38 +0000430vague). It stops being alive and active when its \method{run()}
431method terminates -- either normally, or by raising an unhandled
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000432exception. The \method{isAlive()} method tests whether the thread is
433alive.
434
Fred Drakebf5a6d21999-03-12 19:57:38 +0000435Other threads can call a thread's \method{join()} method. This blocks
436the calling thread until the thread whose \method{join()} method is
437called is terminated.
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000438
439A thread has a name. The name can be passed to the constructor,
Fred Drakebf5a6d21999-03-12 19:57:38 +0000440set with the \method{setName()} method, and retrieved with the
441\method{getName()} method.
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000442
443A thread can be flagged as a ``daemon thread''. The significance
444of this flag is that the entire Python program exits when only
445daemon threads are left. The initial value is inherited from the
Fred Drakebf5a6d21999-03-12 19:57:38 +0000446creating thread. The flag can be set with the \method{setDaemon()}
447method and retrieved with the \method{getDaemon()} method.
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000448
449There is a ``main thread'' object; this corresponds to the
450initial thread of control in the Python program. It is not a
451daemon thread.
452
453There is the possibility that ``dummy thread objects'' are
454created. These are thread objects corresponding to ``alien
455threads''. These are threads of control started outside the
456threading module, e.g. directly from C code. Dummy thread objects
457have limited functionality; they are always considered alive,
Fred Drakebf5a6d21999-03-12 19:57:38 +0000458active, and daemonic, and cannot be \method{join()}ed. They are never
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000459deleted, since it is impossible to detect the termination of alien
460threads.
461
462
463\begin{classdesc}{Thread}{group=None, target=None, name=None,
Fred Drakebf5a6d21999-03-12 19:57:38 +0000464 args=(), kwargs=\{\}}
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000465This constructor should always be called with keyword
466arguments. Arguments are:
467
Fred Drakebf5a6d21999-03-12 19:57:38 +0000468\var{group}
469Should be \code{None}; reserved for future extension when a
470\class{ThreadGroup} class is implemented.
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000471
Fred Drakebf5a6d21999-03-12 19:57:38 +0000472\var{target}
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000473Callable object to be invoked by the \method{run()} method.
Fred Drakebf5a6d21999-03-12 19:57:38 +0000474Defaults to \code{None}, meaning nothing is called.
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000475
Fred Drakebf5a6d21999-03-12 19:57:38 +0000476\var{name}
477The thread name. By default, a unique name is constructed of the form
478``Thread-\var{N}'' where \var{N} is a small decimal number.
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000479
Fred Drakebf5a6d21999-03-12 19:57:38 +0000480\var{args}
481Argument tuple for the target invocation. Defaults to \code{()}.
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000482
Fred Drakebf5a6d21999-03-12 19:57:38 +0000483\var{kwargs}
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000484Keyword argument dictionary for the target invocation.
Fred Drakebf5a6d21999-03-12 19:57:38 +0000485Defaults to \code{\{\}}.
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000486
487If the subclass overrides the constructor, it must make sure
Fred Drakebf5a6d21999-03-12 19:57:38 +0000488to invoke the base class constructor (\code{Thread.__init__()})
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000489before doing anything else to the thread.
490\end{classdesc}
491
492
493
494\begin{methoddesc}{start}{}
495Start the thread's activity.
496
497This must be called at most once per thread object. It
498arranges for the object's \method{run()} method to be invoked in a
499separate thread of control.
500\end{methoddesc}
501
502
503
504\begin{methoddesc}{run}{}
505Method representing the thread's activity.
506
507You may override this method in a subclass. The standard
508\method{run()} method invokes the callable object passed to the object's constructor as the
509\var{target} argument, if any, with sequential and keyword
510arguments taken from the \var{args} and \var{kwargs} arguments,
511respectively.
512\end{methoddesc}
513
514
Fred Drakebf5a6d21999-03-12 19:57:38 +0000515\begin{methoddesc}{join}{\optional{timeout}}
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000516Wait until the thread terminates.
517This blocks the calling thread until the thread whose \method{join()}
518method is called terminates -- either normally or through an
519unhandled exception -- or until the optional timeout occurs.
520
521When the \var{timeout} argument is present and not \code{None}, it should
522be a floating point number specifying a timeout for the
523operation in seconds (or fractions thereof).
524
525A thread can be \method{join()}ed many times.
526
527A thread cannot join itself because this would cause a
528deadlock.
529
530It is an error to attempt to \method{join()} a thread before it has
531been started.
532\end{methoddesc}
533
534
535
536\begin{methoddesc}{getName}{}
537Return the thread's name.
538\end{methoddesc}
539
540\begin{methoddesc}{setName}{name}
541Set the thread's name.
542
543The name is a string used for identification purposes only.
544It has no semantics. Multiple threads may be given the same
545name. The initial name is set by the constructor.
546\end{methoddesc}
547
548\begin{methoddesc}{isAlive}{}
549Return whether the thread is alive.
550
551Roughly, a thread is alive from the moment the \method{start()} method
552returns until its \method{run()} method terminates.
553\end{methoddesc}
554
555\begin{methoddesc}{isDaemon}{}
556Return the thread's daemon flag.
557\end{methoddesc}
558
559\begin{methoddesc}{setDaemon}{daemonic}
560Set the thread's daemon flag to the Boolean value \var{daemonic}.
561This must be called before \method{start()} is called.
562
563The initial value is inherited from the creating thread.
564
565The entire Python program exits when no active non-daemon
566threads are left.
567\end{methoddesc}
568