blob: 27503bdf36bc19e0fffc3fb2f69c495374715851 [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
Fred Drake31d833d2001-08-20 18:49:00 +000063\begin{funcdesc}{Semaphore}{\optional{value}}
Andrew M. Kuchling16440e61998-07-20 13:46:10 +000064A 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
Fred Drake31d833d2001-08-20 18:49:00 +000068without making the counter negative. If not given, \var{value} defaults to
691.
70\end{funcdesc}
71
72\begin{funcdesc}{BoundedSemaphore}{\optional{value}}
73A factory function that returns a new bounded semaphore object. A bounded
74semaphore checks to make sure its current value doesn't exceed its initial
75value. If it does, \exception{ValueError} is raised. In most situations
76semaphores are used to guard resources with limited capacity. If the
77semaphore is released too many times it's a sign of a bug. If not given,
78\var{value} defaults to 1.
Andrew M. Kuchling16440e61998-07-20 13:46:10 +000079\end{funcdesc}
80
Fred Drakec19f3922001-05-31 20:24:07 +000081\begin{classdesc*}{Thread}{}
Andrew M. Kuchling16440e61998-07-20 13:46:10 +000082A class that represents a thread of control. This class can be safely subclassed in a limited fashion.
Fred Drakec19f3922001-05-31 20:24:07 +000083\end{classdesc*}
Andrew M. Kuchling16440e61998-07-20 13:46:10 +000084
85Detailed interfaces for the objects are documented below.
86
87The design of this module is loosely based on Java's threading model.
88However, where Java makes locks and condition variables basic behavior
89of every object, they are separate objects in Python. Python's \class{Thread}
90class supports a subset of the behavior of Java's Thread class;
91currently, there are no priorities, no thread groups, and threads
92cannot be destroyed, stopped, suspended, resumed, or interrupted. The
93static methods of Java's Thread class, when implemented, are mapped to
94module-level functions.
95
96All of the methods described below are executed atomically.
97
Fred Drakebf5a6d21999-03-12 19:57:38 +000098
99\subsection{Lock Objects \label{lock-objects}}
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000100
101A primitive lock is a synchronization primitive that is not owned
102by a particular thread when locked. In Python, it is currently
103the lowest level synchronization primitive available, implemented
Fred Drakeffbe6871999-04-22 21:23:22 +0000104directly by the \refmodule{thread} extension module.
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000105
106A primitive lock is in one of two states, ``locked'' or ``unlocked''.
107It is created in the unlocked state. It has two basic methods,
108\method{acquire()} and \method{release()}. When the state is
109unlocked, \method{acquire()} changes the state to locked and returns
110immediately. When the state is locked, \method{acquire()} blocks
111until a call to \method{release()} in another thread changes it to
112unlocked, then the \method{acquire()} call resets it to locked and
113returns. The \method{release()} method should only be called in the
114locked state; it changes the state to unlocked and returns
115immediately. When more than one thread is blocked in
116\method{acquire()} waiting for the state to turn to unlocked, only one
117thread proceeds when a \method{release()} call resets the state to
118unlocked; which one of the waiting threads proceeds is not defined,
119and may vary across implementations.
120
121All methods are executed atomically.
122
Fred Drakebf5a6d21999-03-12 19:57:38 +0000123\begin{methoddesc}{acquire}{\optional{blocking\code{ = 1}}}
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000124Acquire a lock, blocking or non-blocking.
125
126When invoked without arguments, block until the lock is
127unlocked, then set it to locked, and return. There is no
128return value in this case.
129
130When invoked with the \var{blocking} argument set to true, do the
131same thing as when called without arguments, and return true.
132
133When invoked with the \var{blocking} argument set to false, do not
134block. If a call without an argument would block, return false
135immediately; otherwise, do the same thing as when called
136without arguments, and return true.
137\end{methoddesc}
138
139\begin{methoddesc}{release}{}
140Release a lock.
141
142When the lock is locked, reset it to unlocked, and return. If
143any other threads are blocked waiting for the lock to become
144unlocked, allow exactly one of them to proceed.
145
146Do not call this method when the lock is unlocked.
147
148There is no return value.
149\end{methoddesc}
150
Fred Drakebf5a6d21999-03-12 19:57:38 +0000151
152\subsection{RLock Objects \label{rlock-objects}}
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000153
154A reentrant lock is a synchronization primitive that may be
155acquired multiple times by the same thread. Internally, it uses
156the concepts of ``owning thread'' and ``recursion level'' in
157addition to the locked/unlocked state used by primitive locks. In
158the locked state, some thread owns the lock; in the unlocked
159state, no thread owns it.
160
161To lock the lock, a thread calls its \method{acquire()} method; this
162returns once the thread owns the lock. To unlock the lock, a
Fred Drake907e76b2001-07-06 20:30:11 +0000163thread calls its \method{release()} method.
164\method{acquire()}/\method{release()} call pairs may be nested; only
165the final \method{release()} (the \method{release()} of the outermost
166pair) resets the lock to unlocked and allows another thread blocked in
167\method{acquire()} to proceed.
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000168
Fred Drakebf5a6d21999-03-12 19:57:38 +0000169\begin{methoddesc}{acquire}{\optional{blocking\code{ = 1}}}
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000170Acquire a lock, blocking or non-blocking.
171
172When invoked without arguments: if this thread already owns
173the lock, increment the recursion level by one, and return
174immediately. Otherwise, if another thread owns the lock,
175block until the lock is unlocked. Once the lock is unlocked
176(not owned by any thread), then grab ownership, set the
177recursion level to one, and return. If more than one thread
178is blocked waiting until the lock is unlocked, only one at a
179time will be able to grab ownership of the lock. There is no
180return value in this case.
181
182When invoked with the \var{blocking} argument set to true, do the
183same thing as when called without arguments, and return true.
184
185When invoked with the \var{blocking} argument set to false, do not
186block. If a call without an argument would block, return false
187immediately; otherwise, do the same thing as when called
188without arguments, and return true.
189\end{methoddesc}
190
191\begin{methoddesc}{release}{}
192Release a lock, decrementing the recursion level. If after the
193decrement it is zero, reset the lock to unlocked (not owned by any
194thread), and if any other threads are blocked waiting for the lock to
195become unlocked, allow exactly one of them to proceed. If after the
196decrement the recursion level is still nonzero, the lock remains
197locked and owned by the calling thread.
198
199Only call this method when the calling thread owns the lock.
200Do not call this method when the lock is unlocked.
201
202There is no return value.
203\end{methoddesc}
204
Fred Drakebf5a6d21999-03-12 19:57:38 +0000205
206\subsection{Condition Objects \label{condition-objects}}
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000207
208A condition variable is always associated with some kind of lock;
209this can be passed in or one will be created by default. (Passing
210one in is useful when several condition variables must share the
211same lock.)
212
213A condition variable has \method{acquire()} and \method{release()}
214methods that call the corresponding methods of the associated lock.
215It also has a \method{wait()} method, and \method{notify()} and
216\method{notifyAll()} methods. These three must only be called when
217the calling thread has acquired the lock.
218
219The \method{wait()} method releases the lock, and then blocks until it
220is awakened by a \method{notify()} or \method{notifyAll()} call for
221the same condition variable in another thread. Once awakened, it
222re-acquires the lock and returns. It is also possible to specify a
223timeout.
224
225The \method{notify()} method wakes up one of the threads waiting for
226the condition variable, if any are waiting. The \method{notifyAll()}
227method wakes up all threads waiting for the condition variable.
228
229Note: the \method{notify()} and \method{notifyAll()} methods don't
230release the lock; this means that the thread or threads awakened will
231not return from their \method{wait()} call immediately, but only when
232the thread that called \method{notify()} or \method{notifyAll()}
233finally relinquishes ownership of the lock.
234
235Tip: the typical programming style using condition variables uses the
236lock to synchronize access to some shared state; threads that are
237interested in a particular change of state call \method{wait()}
238repeatedly until they see the desired state, while threads that modify
239the state call \method{notify()} or \method{notifyAll()} when they
240change the state in such a way that it could possibly be a desired
241state for one of the waiters. For example, the following code is a
242generic producer-consumer situation with unlimited buffer capacity:
243
244\begin{verbatim}
245# Consume one item
246cv.acquire()
247while not an_item_is_available():
248 cv.wait()
249get_an_available_item()
250cv.release()
251
252# Produce one item
253cv.acquire()
254make_an_item_available()
255cv.notify()
256cv.release()
257\end{verbatim}
258
259To choose between \method{notify()} and \method{notifyAll()}, consider
260whether one state change can be interesting for only one or several
261waiting threads. E.g. in a typical producer-consumer situation,
262adding one item to the buffer only needs to wake up one consumer
263thread.
264
Fred Drakebf5a6d21999-03-12 19:57:38 +0000265\begin{classdesc}{Condition}{\optional{lock}}
266If the \var{lock} argument is given and not \code{None}, it must be a
267\class{Lock} or \class{RLock} object, and it is used as the underlying
268lock. Otherwise, a new \class{RLock} object is created and used as
269the underlying lock.
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000270\end{classdesc}
271
272\begin{methoddesc}{acquire}{*args}
273Acquire the underlying lock.
274This method calls the corresponding method on the underlying
275lock; the return value is whatever that method returns.
276\end{methoddesc}
277
278\begin{methoddesc}{release}{}
279Release the underlying lock.
280This method calls the corresponding method on the underlying
281lock; there is no return value.
282\end{methoddesc}
283
Fred Drakebf5a6d21999-03-12 19:57:38 +0000284\begin{methoddesc}{wait}{\optional{timeout}}
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000285Wait until notified or until a timeout occurs.
286This must only be called when the calling thread has acquired the
287lock.
288
289This method releases the underlying lock, and then blocks until it is
290awakened by a \method{notify()} or \method{notifyAll()} call for the
291same condition variable in another thread, or until the optional
292timeout occurs. Once awakened or timed out, it re-acquires the lock
293and returns.
294
Fred Drakebf5a6d21999-03-12 19:57:38 +0000295When the \var{timeout} argument is present and not \code{None}, it
296should be a floating point number specifying a timeout for the
297operation in seconds (or fractions thereof).
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000298
Fred Drakebf5a6d21999-03-12 19:57:38 +0000299When the underlying lock is an \class{RLock}, it is not released using
300its \method{release()} method, since this may not actually unlock the
301lock when it was acquired multiple times recursively. Instead, an
302internal interface of the \class{RLock} class is used, which really
303unlocks it even when it has been recursively acquired several times.
304Another internal interface is then used to restore the recursion level
305when the lock is reacquired.
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000306\end{methoddesc}
307
308\begin{methoddesc}{notify}{}
309Wake up a thread waiting on this condition, if any.
310This must only be called when the calling thread has acquired the
311lock.
312
313This method wakes up one of the threads waiting for the condition
314variable, if any are waiting; it is a no-op if no threads are waiting.
315
316The current implementation wakes up exactly one thread, if any are
317waiting. However, it's not safe to rely on this behavior. A future,
318optimized implementation may occasionally wake up more than one
319thread.
320
321Note: the awakened thread does not actually return from its
322\method{wait()} call until it can reacquire the lock. Since
323\method{notify()} does not release the lock, its caller should.
324\end{methoddesc}
325
326\begin{methoddesc}{notifyAll}{}
327Wake up all threads waiting on this condition. This method acts like
328\method{notify()}, but wakes up all waiting threads instead of one.
329\end{methoddesc}
330
Fred Drakebf5a6d21999-03-12 19:57:38 +0000331
332\subsection{Semaphore Objects \label{semaphore-objects}}
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000333
334This is one of the oldest synchronization primitives in the history of
335computer science, invented by the early Dutch computer scientist
Fred Drakebf5a6d21999-03-12 19:57:38 +0000336Edsger W. Dijkstra (he used \method{P()} and \method{V()} instead of
337\method{acquire()} and \method{release()}).
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000338
339A semaphore manages an internal counter which is decremented by each
340\method{acquire()} call and incremented by each \method{release()}
341call. The counter can never go below zero; when \method{acquire()}
342finds that it is zero, it blocks, waiting until some other thread
343calls \method{release()}.
344
Fred Drakebf5a6d21999-03-12 19:57:38 +0000345\begin{classdesc}{Semaphore}{\optional{value}}
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000346The optional argument gives the initial value for the internal
Fred Drakebf5a6d21999-03-12 19:57:38 +0000347counter; it defaults to \code{1}.
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000348\end{classdesc}
349
Fred Drakebf5a6d21999-03-12 19:57:38 +0000350\begin{methoddesc}{acquire}{\optional{blocking}}
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000351Acquire a semaphore.
352
353When invoked without arguments: if the internal counter is larger than
354zero on entry, decrement it by one and return immediately. If it is
355zero on entry, block, waiting until some other thread has called
356\method{release()} to make it larger than zero. This is done with
357proper interlocking so that if multiple \method{acquire()} calls are
358blocked, \method{release()} will wake exactly one of them up. The
359implementation may pick one at random, so the order in which blocked
360threads are awakened should not be relied on. There is no return
361value in this case.
362
Fred Drakebf5a6d21999-03-12 19:57:38 +0000363When invoked with \var{blocking} set to true, do the same thing as
364when called without arguments, and return true.
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000365
Fred Drakebf5a6d21999-03-12 19:57:38 +0000366When invoked with \var{blocking} set to false, do not block. If a
367call without an argument would block, return false immediately;
368otherwise, do the same thing as when called without arguments, and
369return true.
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000370\end{methoddesc}
371
372\begin{methoddesc}{release}{}
373Release a semaphore,
374incrementing the internal counter by one. When it was zero on
375entry and another thread is waiting for it to become larger
376than zero again, wake up that thread.
377\end{methoddesc}
378
Fred Drakebf5a6d21999-03-12 19:57:38 +0000379
Fred Drake31d833d2001-08-20 18:49:00 +0000380\subsubsection{\class{Semaphore} Example \label{semaphore-examples}}
381
382Semaphores are often used to guard resources with limited capacity, for
383example, a database server. In any situation where the size of the resource
384size is fixed, you should use a bounded semaphore. Before spawning any
385worker threads, your main thread would initialize the semaphore:
386
387\begin{verbatim}
388maxconnections = 5
389...
390pool_sema = BoundedSemaphore(value=maxconnections)
391\end{verbatim}
392
393Once spawned, worker threads call the semaphore's acquire and release
394methods when they need to connect to the server:
395
396\begin{verbatim}
397pool_sema.acquire()
398conn = connectdb()
399... use connection ...
400conn.close()
401pool_sema.release()
402\end{verbatim}
403
404The use of a bounded semaphore reduces the chance that a programming error
405which causes the semaphore to be released more than it's acquired will go
406undetected.
407
Fred Drakebf5a6d21999-03-12 19:57:38 +0000408\subsection{Event Objects \label{event-objects}}
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000409
410This is one of the simplest mechanisms for communication between
Fred Drake86119211999-04-23 20:07:02 +0000411threads: one thread signals an event and one or more other threads
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000412are waiting for it.
413
414An event object manages an internal flag that can be set to true with
415the \method{set()} method and reset to false with the \method{clear()} method. The
416\method{wait()} method blocks until the flag is true.
417
418
419\begin{classdesc}{Event}{}
420The internal flag is initially false.
421\end{classdesc}
422
423\begin{methoddesc}{isSet}{}
424Return true if and only if the internal flag is true.
425\end{methoddesc}
426
427\begin{methoddesc}{set}{}
428Set the internal flag to true.
429All threads waiting for it to become true are awakened.
430Threads that call \method{wait()} once the flag is true will not block
431at all.
432\end{methoddesc}
433
434\begin{methoddesc}{clear}{}
435Reset the internal flag to false.
436Subsequently, threads calling \method{wait()} will block until \method{set()} is
437called to set the internal flag to true again.
438\end{methoddesc}
439
Fred Drakebf5a6d21999-03-12 19:57:38 +0000440\begin{methoddesc}{wait}{\optional{timeout}}
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000441Block until the internal flag is true.
442If the internal flag is true on entry, return immediately. Otherwise,
443block until another thread calls \method{set()} to set the flag to
444true, or until the optional timeout occurs.
445
446When the timeout argument is present and not \code{None}, it should be a
447floating point number specifying a timeout for the operation in
448seconds (or fractions thereof).
449\end{methoddesc}
450
Fred Drakebf5a6d21999-03-12 19:57:38 +0000451
452\subsection{Thread Objects \label{thread-objects}}
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000453
454This class represents an activity that is run in a separate thread
455of control. There are two ways to specify the activity: by
456passing a callable object to the constructor, or by overriding the
457\method{run()} method in a subclass. No other methods (except for the
458constructor) should be overridden in a subclass. In other words,
Fred Drakebf5a6d21999-03-12 19:57:38 +0000459\emph{only} override the \method{__init__()} and \method{run()}
460methods of this class.
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000461
462Once a thread object is created, its activity must be started by
Fred Drakebf5a6d21999-03-12 19:57:38 +0000463calling the thread's \method{start()} method. This invokes the
464\method{run()} method in a separate thread of control.
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000465
466Once the thread's activity is started, the thread is considered
467'alive' and 'active' (these concepts are almost, but not quite
468exactly, the same; their definition is intentionally somewhat
Fred Drakebf5a6d21999-03-12 19:57:38 +0000469vague). It stops being alive and active when its \method{run()}
470method terminates -- either normally, or by raising an unhandled
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000471exception. The \method{isAlive()} method tests whether the thread is
472alive.
473
Fred Drakebf5a6d21999-03-12 19:57:38 +0000474Other threads can call a thread's \method{join()} method. This blocks
475the calling thread until the thread whose \method{join()} method is
476called is terminated.
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000477
478A thread has a name. The name can be passed to the constructor,
Fred Drakebf5a6d21999-03-12 19:57:38 +0000479set with the \method{setName()} method, and retrieved with the
480\method{getName()} method.
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000481
482A thread can be flagged as a ``daemon thread''. The significance
483of this flag is that the entire Python program exits when only
484daemon threads are left. The initial value is inherited from the
Fred Drakebf5a6d21999-03-12 19:57:38 +0000485creating thread. The flag can be set with the \method{setDaemon()}
Fred Drakec19f3922001-05-31 20:24:07 +0000486method and retrieved with the \method{isDaemon()} method.
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000487
488There is a ``main thread'' object; this corresponds to the
489initial thread of control in the Python program. It is not a
490daemon thread.
491
492There is the possibility that ``dummy thread objects'' are
493created. These are thread objects corresponding to ``alien
494threads''. These are threads of control started outside the
Fred Drake907e76b2001-07-06 20:30:11 +0000495threading module, such as directly from C code. Dummy thread objects
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000496have limited functionality; they are always considered alive,
Fred Drakebf5a6d21999-03-12 19:57:38 +0000497active, and daemonic, and cannot be \method{join()}ed. They are never
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000498deleted, since it is impossible to detect the termination of alien
499threads.
500
501
502\begin{classdesc}{Thread}{group=None, target=None, name=None,
Fred Drakebf5a6d21999-03-12 19:57:38 +0000503 args=(), kwargs=\{\}}
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000504This constructor should always be called with keyword
505arguments. Arguments are:
506
Fred Drakec19f3922001-05-31 20:24:07 +0000507\var{group} should be \code{None}; reserved for future extension when
508a \class{ThreadGroup} class is implemented.
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000509
Fred Drakec19f3922001-05-31 20:24:07 +0000510\var{target} is the callable object to be invoked by the
511\method{run()} method. Defaults to \code{None}, meaning nothing is
512called.
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000513
Fred Drakec19f3922001-05-31 20:24:07 +0000514\var{name} is the thread name. By default, a unique name is
515constructed of the form ``Thread-\var{N}'' where \var{N} is a small
516decimal number.
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000517
Fred Drakec19f3922001-05-31 20:24:07 +0000518\var{args} is the argument tuple for the target invocation. Defaults
519to \code{()}.
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000520
Fred Drakec19f3922001-05-31 20:24:07 +0000521\var{kwargs} is a dictionary of keyword arguments for the target
522invocation. Defaults to \code{\{\}}.
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000523
524If the subclass overrides the constructor, it must make sure
Fred Drakebf5a6d21999-03-12 19:57:38 +0000525to invoke the base class constructor (\code{Thread.__init__()})
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000526before doing anything else to the thread.
527\end{classdesc}
528
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000529\begin{methoddesc}{start}{}
530Start the thread's activity.
531
532This must be called at most once per thread object. It
533arranges for the object's \method{run()} method to be invoked in a
534separate thread of control.
535\end{methoddesc}
536
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000537\begin{methoddesc}{run}{}
538Method representing the thread's activity.
539
540You may override this method in a subclass. The standard
541\method{run()} method invokes the callable object passed to the object's constructor as the
542\var{target} argument, if any, with sequential and keyword
543arguments taken from the \var{args} and \var{kwargs} arguments,
544respectively.
545\end{methoddesc}
546
Fred Drakebf5a6d21999-03-12 19:57:38 +0000547\begin{methoddesc}{join}{\optional{timeout}}
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000548Wait until the thread terminates.
549This blocks the calling thread until the thread whose \method{join()}
550method is called terminates -- either normally or through an
551unhandled exception -- or until the optional timeout occurs.
552
553When the \var{timeout} argument is present and not \code{None}, it should
554be a floating point number specifying a timeout for the
555operation in seconds (or fractions thereof).
556
557A thread can be \method{join()}ed many times.
558
559A thread cannot join itself because this would cause a
560deadlock.
561
562It is an error to attempt to \method{join()} a thread before it has
563been started.
564\end{methoddesc}
565
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000566\begin{methoddesc}{getName}{}
567Return the thread's name.
568\end{methoddesc}
569
570\begin{methoddesc}{setName}{name}
571Set the thread's name.
572
573The name is a string used for identification purposes only.
574It has no semantics. Multiple threads may be given the same
575name. The initial name is set by the constructor.
576\end{methoddesc}
577
578\begin{methoddesc}{isAlive}{}
579Return whether the thread is alive.
580
581Roughly, a thread is alive from the moment the \method{start()} method
582returns until its \method{run()} method terminates.
583\end{methoddesc}
584
585\begin{methoddesc}{isDaemon}{}
586Return the thread's daemon flag.
587\end{methoddesc}
588
589\begin{methoddesc}{setDaemon}{daemonic}
590Set the thread's daemon flag to the Boolean value \var{daemonic}.
591This must be called before \method{start()} is called.
592
593The initial value is inherited from the creating thread.
594
595The entire Python program exits when no active non-daemon
596threads are left.
597\end{methoddesc}