blob: 2fe72de91c4f34fa47466b7221c0f54c600e4a90 [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 Drake740f8002002-12-30 23:00:36 +000011The \refmodule[dummythreading]{dummy_threading} module is provided for
12situations where \module{threading} cannot be used because
13\refmodule{thread} is missing.
Guido van Rossum29692332002-12-30 22:34:10 +000014
Fred Drake11f89b72003-01-06 16:38:10 +000015This module defines the following functions and objects:
Andrew M. Kuchling16440e61998-07-20 13:46:10 +000016
17\begin{funcdesc}{activeCount}{}
18Return the number of currently active \class{Thread} objects.
19The returned count is equal to the length of the list returned by
20\function{enumerate()}.
21A function that returns the number of currently active threads.
22\end{funcdesc}
23
24\begin{funcdesc}{Condition}{}
25A factory function that returns a new condition variable object.
26A condition variable allows one or more threads to wait until they
27are notified by another thread.
28\end{funcdesc}
29
30\begin{funcdesc}{currentThread}{}
31Return the current \class{Thread} object, corresponding to the
32caller's thread of control. If the caller's thread of control was not
33created through the
34\module{threading} module, a dummy thread object with limited functionality
35is returned.
36\end{funcdesc}
37
38\begin{funcdesc}{enumerate}{}
39Return a list of all currently active \class{Thread} objects.
40The list includes daemonic threads, dummy thread objects created
41by \function{currentThread()}, and the main thread. It excludes terminated
42threads and threads that have not yet been started.
43\end{funcdesc}
44
45\begin{funcdesc}{Event}{}
Fred Drake12686782002-03-19 14:37:44 +000046A factory function that returns a new event object. An event manages
47a flag that can be set to true with the \method{set()} method and
48reset to false with the \method{clear()} method. The \method{wait()}
49method blocks until the flag is true.
Andrew M. Kuchling16440e61998-07-20 13:46:10 +000050\end{funcdesc}
51
52\begin{funcdesc}{Lock}{}
53A factory function that returns a new primitive lock object. Once
54a thread has acquired it, subsequent attempts to acquire it block,
55until it is released; any thread may release it.
56\end{funcdesc}
57
58\begin{funcdesc}{RLock}{}
59A factory function that returns a new reentrant lock object.
60A reentrant lock must be released by the thread that acquired it.
61Once a thread has acquired a reentrant lock, the same thread may
62acquire it again without blocking; the thread must release it once
63for each time it has acquired it.
64\end{funcdesc}
65
Fred Drake31d833d2001-08-20 18:49:00 +000066\begin{funcdesc}{Semaphore}{\optional{value}}
Andrew M. Kuchling16440e61998-07-20 13:46:10 +000067A factory function that returns a new semaphore object. A
68semaphore manages a counter representing the number of \method{release()}
69calls minus the number of \method{acquire()} calls, plus an initial value.
70The \method{acquire()} method blocks if necessary until it can return
Fred Drake31d833d2001-08-20 18:49:00 +000071without making the counter negative. If not given, \var{value} defaults to
721.
73\end{funcdesc}
74
75\begin{funcdesc}{BoundedSemaphore}{\optional{value}}
76A factory function that returns a new bounded semaphore object. A bounded
77semaphore checks to make sure its current value doesn't exceed its initial
78value. If it does, \exception{ValueError} is raised. In most situations
79semaphores are used to guard resources with limited capacity. If the
80semaphore is released too many times it's a sign of a bug. If not given,
81\var{value} defaults to 1.
Andrew M. Kuchling16440e61998-07-20 13:46:10 +000082\end{funcdesc}
83
Fred Drakec19f3922001-05-31 20:24:07 +000084\begin{classdesc*}{Thread}{}
Fred Drake12686782002-03-19 14:37:44 +000085A class that represents a thread of control. This class can be safely
86subclassed in a limited fashion.
Fred Drakec19f3922001-05-31 20:24:07 +000087\end{classdesc*}
Andrew M. Kuchling16440e61998-07-20 13:46:10 +000088
Martin v. Löwis44f86962001-09-05 13:44:54 +000089\begin{classdesc*}{Timer}{}
90A thread that executes a function after a specified interval has passed.
91\end{classdesc*}
92
Andrew M. Kuchling16440e61998-07-20 13:46:10 +000093Detailed interfaces for the objects are documented below.
94
95The design of this module is loosely based on Java's threading model.
96However, where Java makes locks and condition variables basic behavior
97of every object, they are separate objects in Python. Python's \class{Thread}
98class supports a subset of the behavior of Java's Thread class;
99currently, there are no priorities, no thread groups, and threads
100cannot be destroyed, stopped, suspended, resumed, or interrupted. The
101static methods of Java's Thread class, when implemented, are mapped to
102module-level functions.
103
104All of the methods described below are executed atomically.
105
Fred Drakebf5a6d21999-03-12 19:57:38 +0000106
107\subsection{Lock Objects \label{lock-objects}}
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000108
109A primitive lock is a synchronization primitive that is not owned
110by a particular thread when locked. In Python, it is currently
111the lowest level synchronization primitive available, implemented
Fred Drakeffbe6871999-04-22 21:23:22 +0000112directly by the \refmodule{thread} extension module.
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000113
114A primitive lock is in one of two states, ``locked'' or ``unlocked''.
115It is created in the unlocked state. It has two basic methods,
116\method{acquire()} and \method{release()}. When the state is
117unlocked, \method{acquire()} changes the state to locked and returns
118immediately. When the state is locked, \method{acquire()} blocks
119until a call to \method{release()} in another thread changes it to
120unlocked, then the \method{acquire()} call resets it to locked and
121returns. The \method{release()} method should only be called in the
122locked state; it changes the state to unlocked and returns
123immediately. When more than one thread is blocked in
124\method{acquire()} waiting for the state to turn to unlocked, only one
125thread proceeds when a \method{release()} call resets the state to
126unlocked; which one of the waiting threads proceeds is not defined,
127and may vary across implementations.
128
129All methods are executed atomically.
130
Fred Drakebf5a6d21999-03-12 19:57:38 +0000131\begin{methoddesc}{acquire}{\optional{blocking\code{ = 1}}}
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000132Acquire a lock, blocking or non-blocking.
133
134When invoked without arguments, block until the lock is
135unlocked, then set it to locked, and return. There is no
136return value in this case.
137
138When invoked with the \var{blocking} argument set to true, do the
139same thing as when called without arguments, and return true.
140
141When invoked with the \var{blocking} argument set to false, do not
142block. If a call without an argument would block, return false
143immediately; otherwise, do the same thing as when called
144without arguments, and return true.
145\end{methoddesc}
146
147\begin{methoddesc}{release}{}
148Release a lock.
149
150When the lock is locked, reset it to unlocked, and return. If
151any other threads are blocked waiting for the lock to become
152unlocked, allow exactly one of them to proceed.
153
154Do not call this method when the lock is unlocked.
155
156There is no return value.
157\end{methoddesc}
158
Fred Drakebf5a6d21999-03-12 19:57:38 +0000159
160\subsection{RLock Objects \label{rlock-objects}}
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000161
162A reentrant lock is a synchronization primitive that may be
163acquired multiple times by the same thread. Internally, it uses
164the concepts of ``owning thread'' and ``recursion level'' in
165addition to the locked/unlocked state used by primitive locks. In
166the locked state, some thread owns the lock; in the unlocked
167state, no thread owns it.
168
169To lock the lock, a thread calls its \method{acquire()} method; this
170returns once the thread owns the lock. To unlock the lock, a
Fred Drake907e76b2001-07-06 20:30:11 +0000171thread calls its \method{release()} method.
172\method{acquire()}/\method{release()} call pairs may be nested; only
173the final \method{release()} (the \method{release()} of the outermost
174pair) resets the lock to unlocked and allows another thread blocked in
175\method{acquire()} to proceed.
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000176
Fred Drakebf5a6d21999-03-12 19:57:38 +0000177\begin{methoddesc}{acquire}{\optional{blocking\code{ = 1}}}
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000178Acquire a lock, blocking or non-blocking.
179
180When invoked without arguments: if this thread already owns
181the lock, increment the recursion level by one, and return
182immediately. Otherwise, if another thread owns the lock,
183block until the lock is unlocked. Once the lock is unlocked
184(not owned by any thread), then grab ownership, set the
185recursion level to one, and return. If more than one thread
186is blocked waiting until the lock is unlocked, only one at a
187time will be able to grab ownership of the lock. There is no
188return value in this case.
189
190When invoked with the \var{blocking} argument set to true, do the
191same thing as when called without arguments, and return true.
192
193When invoked with the \var{blocking} argument set to false, do not
194block. If a call without an argument would block, return false
195immediately; otherwise, do the same thing as when called
196without arguments, and return true.
197\end{methoddesc}
198
199\begin{methoddesc}{release}{}
200Release a lock, decrementing the recursion level. If after the
201decrement it is zero, reset the lock to unlocked (not owned by any
202thread), and if any other threads are blocked waiting for the lock to
203become unlocked, allow exactly one of them to proceed. If after the
204decrement the recursion level is still nonzero, the lock remains
205locked and owned by the calling thread.
206
207Only call this method when the calling thread owns the lock.
208Do not call this method when the lock is unlocked.
209
210There is no return value.
211\end{methoddesc}
212
Fred Drakebf5a6d21999-03-12 19:57:38 +0000213
214\subsection{Condition Objects \label{condition-objects}}
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000215
216A condition variable is always associated with some kind of lock;
217this can be passed in or one will be created by default. (Passing
218one in is useful when several condition variables must share the
219same lock.)
220
221A condition variable has \method{acquire()} and \method{release()}
222methods that call the corresponding methods of the associated lock.
223It also has a \method{wait()} method, and \method{notify()} and
224\method{notifyAll()} methods. These three must only be called when
225the calling thread has acquired the lock.
226
227The \method{wait()} method releases the lock, and then blocks until it
228is awakened by a \method{notify()} or \method{notifyAll()} call for
229the same condition variable in another thread. Once awakened, it
230re-acquires the lock and returns. It is also possible to specify a
231timeout.
232
233The \method{notify()} method wakes up one of the threads waiting for
234the condition variable, if any are waiting. The \method{notifyAll()}
235method wakes up all threads waiting for the condition variable.
236
237Note: the \method{notify()} and \method{notifyAll()} methods don't
238release the lock; this means that the thread or threads awakened will
239not return from their \method{wait()} call immediately, but only when
240the thread that called \method{notify()} or \method{notifyAll()}
241finally relinquishes ownership of the lock.
242
243Tip: the typical programming style using condition variables uses the
244lock to synchronize access to some shared state; threads that are
245interested in a particular change of state call \method{wait()}
246repeatedly until they see the desired state, while threads that modify
247the state call \method{notify()} or \method{notifyAll()} when they
248change the state in such a way that it could possibly be a desired
249state for one of the waiters. For example, the following code is a
250generic producer-consumer situation with unlimited buffer capacity:
251
252\begin{verbatim}
253# Consume one item
254cv.acquire()
255while not an_item_is_available():
256 cv.wait()
257get_an_available_item()
258cv.release()
259
260# Produce one item
261cv.acquire()
262make_an_item_available()
263cv.notify()
264cv.release()
265\end{verbatim}
266
267To choose between \method{notify()} and \method{notifyAll()}, consider
268whether one state change can be interesting for only one or several
269waiting threads. E.g. in a typical producer-consumer situation,
270adding one item to the buffer only needs to wake up one consumer
271thread.
272
Fred Drakebf5a6d21999-03-12 19:57:38 +0000273\begin{classdesc}{Condition}{\optional{lock}}
274If the \var{lock} argument is given and not \code{None}, it must be a
275\class{Lock} or \class{RLock} object, and it is used as the underlying
276lock. Otherwise, a new \class{RLock} object is created and used as
277the underlying lock.
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000278\end{classdesc}
279
280\begin{methoddesc}{acquire}{*args}
281Acquire the underlying lock.
282This method calls the corresponding method on the underlying
283lock; the return value is whatever that method returns.
284\end{methoddesc}
285
286\begin{methoddesc}{release}{}
287Release the underlying lock.
288This method calls the corresponding method on the underlying
289lock; there is no return value.
290\end{methoddesc}
291
Fred Drakebf5a6d21999-03-12 19:57:38 +0000292\begin{methoddesc}{wait}{\optional{timeout}}
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000293Wait until notified or until a timeout occurs.
294This must only be called when the calling thread has acquired the
295lock.
296
297This method releases the underlying lock, and then blocks until it is
298awakened by a \method{notify()} or \method{notifyAll()} call for the
299same condition variable in another thread, or until the optional
300timeout occurs. Once awakened or timed out, it re-acquires the lock
301and returns.
302
Fred Drakebf5a6d21999-03-12 19:57:38 +0000303When the \var{timeout} argument is present and not \code{None}, it
304should be a floating point number specifying a timeout for the
305operation in seconds (or fractions thereof).
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000306
Fred Drakebf5a6d21999-03-12 19:57:38 +0000307When the underlying lock is an \class{RLock}, it is not released using
308its \method{release()} method, since this may not actually unlock the
309lock when it was acquired multiple times recursively. Instead, an
310internal interface of the \class{RLock} class is used, which really
311unlocks it even when it has been recursively acquired several times.
312Another internal interface is then used to restore the recursion level
313when the lock is reacquired.
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000314\end{methoddesc}
315
316\begin{methoddesc}{notify}{}
317Wake up a thread waiting on this condition, if any.
318This must only be called when the calling thread has acquired the
319lock.
320
321This method wakes up one of the threads waiting for the condition
322variable, if any are waiting; it is a no-op if no threads are waiting.
323
324The current implementation wakes up exactly one thread, if any are
325waiting. However, it's not safe to rely on this behavior. A future,
326optimized implementation may occasionally wake up more than one
327thread.
328
329Note: the awakened thread does not actually return from its
330\method{wait()} call until it can reacquire the lock. Since
331\method{notify()} does not release the lock, its caller should.
332\end{methoddesc}
333
334\begin{methoddesc}{notifyAll}{}
335Wake up all threads waiting on this condition. This method acts like
336\method{notify()}, but wakes up all waiting threads instead of one.
337\end{methoddesc}
338
Fred Drakebf5a6d21999-03-12 19:57:38 +0000339
340\subsection{Semaphore Objects \label{semaphore-objects}}
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000341
342This is one of the oldest synchronization primitives in the history of
343computer science, invented by the early Dutch computer scientist
Fred Drakebf5a6d21999-03-12 19:57:38 +0000344Edsger W. Dijkstra (he used \method{P()} and \method{V()} instead of
345\method{acquire()} and \method{release()}).
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000346
347A semaphore manages an internal counter which is decremented by each
348\method{acquire()} call and incremented by each \method{release()}
349call. The counter can never go below zero; when \method{acquire()}
350finds that it is zero, it blocks, waiting until some other thread
351calls \method{release()}.
352
Fred Drakebf5a6d21999-03-12 19:57:38 +0000353\begin{classdesc}{Semaphore}{\optional{value}}
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000354The optional argument gives the initial value for the internal
Fred Drakebf5a6d21999-03-12 19:57:38 +0000355counter; it defaults to \code{1}.
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000356\end{classdesc}
357
Fred Drakebf5a6d21999-03-12 19:57:38 +0000358\begin{methoddesc}{acquire}{\optional{blocking}}
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000359Acquire a semaphore.
360
361When invoked without arguments: if the internal counter is larger than
362zero on entry, decrement it by one and return immediately. If it is
363zero on entry, block, waiting until some other thread has called
364\method{release()} to make it larger than zero. This is done with
365proper interlocking so that if multiple \method{acquire()} calls are
366blocked, \method{release()} will wake exactly one of them up. The
367implementation may pick one at random, so the order in which blocked
368threads are awakened should not be relied on. There is no return
369value in this case.
370
Fred Drakebf5a6d21999-03-12 19:57:38 +0000371When invoked with \var{blocking} set to true, do the same thing as
372when called without arguments, and return true.
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000373
Fred Drakebf5a6d21999-03-12 19:57:38 +0000374When invoked with \var{blocking} set to false, do not block. If a
375call without an argument would block, return false immediately;
376otherwise, do the same thing as when called without arguments, and
377return true.
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000378\end{methoddesc}
379
380\begin{methoddesc}{release}{}
381Release a semaphore,
382incrementing the internal counter by one. When it was zero on
383entry and another thread is waiting for it to become larger
384than zero again, wake up that thread.
385\end{methoddesc}
386
Fred Drakebf5a6d21999-03-12 19:57:38 +0000387
Fred Drake31d833d2001-08-20 18:49:00 +0000388\subsubsection{\class{Semaphore} Example \label{semaphore-examples}}
389
390Semaphores are often used to guard resources with limited capacity, for
391example, a database server. In any situation where the size of the resource
392size is fixed, you should use a bounded semaphore. Before spawning any
393worker threads, your main thread would initialize the semaphore:
394
395\begin{verbatim}
396maxconnections = 5
397...
398pool_sema = BoundedSemaphore(value=maxconnections)
399\end{verbatim}
400
401Once spawned, worker threads call the semaphore's acquire and release
402methods when they need to connect to the server:
403
404\begin{verbatim}
405pool_sema.acquire()
406conn = connectdb()
407... use connection ...
408conn.close()
409pool_sema.release()
410\end{verbatim}
411
412The use of a bounded semaphore reduces the chance that a programming error
413which causes the semaphore to be released more than it's acquired will go
414undetected.
415
Fred Drake12686782002-03-19 14:37:44 +0000416
Fred Drakebf5a6d21999-03-12 19:57:38 +0000417\subsection{Event Objects \label{event-objects}}
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000418
419This is one of the simplest mechanisms for communication between
Fred Drake12686782002-03-19 14:37:44 +0000420threads: one thread signals an event and other threads wait for it.
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000421
422An event object manages an internal flag that can be set to true with
Fred Drake12686782002-03-19 14:37:44 +0000423the \method{set()} method and reset to false with the \method{clear()}
424method. The \method{wait()} method blocks until the flag is true.
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000425
426
427\begin{classdesc}{Event}{}
428The internal flag is initially false.
429\end{classdesc}
430
431\begin{methoddesc}{isSet}{}
432Return true if and only if the internal flag is true.
433\end{methoddesc}
434
435\begin{methoddesc}{set}{}
436Set the internal flag to true.
437All threads waiting for it to become true are awakened.
438Threads that call \method{wait()} once the flag is true will not block
439at all.
440\end{methoddesc}
441
442\begin{methoddesc}{clear}{}
443Reset the internal flag to false.
Fred Drake12686782002-03-19 14:37:44 +0000444Subsequently, threads calling \method{wait()} will block until
445\method{set()} is called to set the internal flag to true again.
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000446\end{methoddesc}
447
Fred Drakebf5a6d21999-03-12 19:57:38 +0000448\begin{methoddesc}{wait}{\optional{timeout}}
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000449Block until the internal flag is true.
450If the internal flag is true on entry, return immediately. Otherwise,
451block until another thread calls \method{set()} to set the flag to
452true, or until the optional timeout occurs.
453
454When the timeout argument is present and not \code{None}, it should be a
455floating point number specifying a timeout for the operation in
456seconds (or fractions thereof).
457\end{methoddesc}
458
Fred Drakebf5a6d21999-03-12 19:57:38 +0000459
460\subsection{Thread Objects \label{thread-objects}}
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000461
462This class represents an activity that is run in a separate thread
463of control. There are two ways to specify the activity: by
464passing a callable object to the constructor, or by overriding the
465\method{run()} method in a subclass. No other methods (except for the
466constructor) should be overridden in a subclass. In other words,
Fred Drakebf5a6d21999-03-12 19:57:38 +0000467\emph{only} override the \method{__init__()} and \method{run()}
468methods of this class.
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000469
470Once a thread object is created, its activity must be started by
Fred Drakebf5a6d21999-03-12 19:57:38 +0000471calling the thread's \method{start()} method. This invokes the
472\method{run()} method in a separate thread of control.
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000473
474Once the thread's activity is started, the thread is considered
475'alive' and 'active' (these concepts are almost, but not quite
476exactly, the same; their definition is intentionally somewhat
Fred Drakebf5a6d21999-03-12 19:57:38 +0000477vague). It stops being alive and active when its \method{run()}
478method terminates -- either normally, or by raising an unhandled
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000479exception. The \method{isAlive()} method tests whether the thread is
480alive.
481
Fred Drakebf5a6d21999-03-12 19:57:38 +0000482Other threads can call a thread's \method{join()} method. This blocks
483the calling thread until the thread whose \method{join()} method is
484called is terminated.
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000485
486A thread has a name. The name can be passed to the constructor,
Fred Drakebf5a6d21999-03-12 19:57:38 +0000487set with the \method{setName()} method, and retrieved with the
488\method{getName()} method.
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000489
490A thread can be flagged as a ``daemon thread''. The significance
491of this flag is that the entire Python program exits when only
492daemon threads are left. The initial value is inherited from the
Fred Drakebf5a6d21999-03-12 19:57:38 +0000493creating thread. The flag can be set with the \method{setDaemon()}
Fred Drakec19f3922001-05-31 20:24:07 +0000494method and retrieved with the \method{isDaemon()} method.
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000495
496There is a ``main thread'' object; this corresponds to the
497initial thread of control in the Python program. It is not a
498daemon thread.
499
500There is the possibility that ``dummy thread objects'' are
501created. These are thread objects corresponding to ``alien
502threads''. These are threads of control started outside the
Fred Drake907e76b2001-07-06 20:30:11 +0000503threading module, such as directly from C code. Dummy thread objects
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000504have limited functionality; they are always considered alive,
Fred Drakebf5a6d21999-03-12 19:57:38 +0000505active, and daemonic, and cannot be \method{join()}ed. They are never
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000506deleted, since it is impossible to detect the termination of alien
507threads.
508
509
510\begin{classdesc}{Thread}{group=None, target=None, name=None,
Fred Drakebf5a6d21999-03-12 19:57:38 +0000511 args=(), kwargs=\{\}}
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000512This constructor should always be called with keyword
513arguments. Arguments are:
514
Fred Drakec19f3922001-05-31 20:24:07 +0000515\var{group} should be \code{None}; reserved for future extension when
516a \class{ThreadGroup} class is implemented.
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000517
Fred Drakec19f3922001-05-31 20:24:07 +0000518\var{target} is the callable object to be invoked by the
519\method{run()} method. Defaults to \code{None}, meaning nothing is
520called.
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000521
Fred Drakec19f3922001-05-31 20:24:07 +0000522\var{name} is the thread name. By default, a unique name is
523constructed of the form ``Thread-\var{N}'' where \var{N} is a small
524decimal number.
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000525
Fred Drakec19f3922001-05-31 20:24:07 +0000526\var{args} is the argument tuple for the target invocation. Defaults
527to \code{()}.
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000528
Fred Drakec19f3922001-05-31 20:24:07 +0000529\var{kwargs} is a dictionary of keyword arguments for the target
530invocation. Defaults to \code{\{\}}.
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000531
532If the subclass overrides the constructor, it must make sure
Fred Drakebf5a6d21999-03-12 19:57:38 +0000533to invoke the base class constructor (\code{Thread.__init__()})
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000534before doing anything else to the thread.
535\end{classdesc}
536
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000537\begin{methoddesc}{start}{}
538Start the thread's activity.
539
540This must be called at most once per thread object. It
541arranges for the object's \method{run()} method to be invoked in a
542separate thread of control.
543\end{methoddesc}
544
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000545\begin{methoddesc}{run}{}
546Method representing the thread's activity.
547
548You may override this method in a subclass. The standard
Fred Drake12686782002-03-19 14:37:44 +0000549\method{run()} method invokes the callable object passed to the
550object's constructor as the \var{target} argument, if any, with
551sequential and keyword arguments taken from the \var{args} and
552\var{kwargs} arguments, respectively.
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000553\end{methoddesc}
554
Fred Drakebf5a6d21999-03-12 19:57:38 +0000555\begin{methoddesc}{join}{\optional{timeout}}
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000556Wait until the thread terminates.
557This blocks the calling thread until the thread whose \method{join()}
558method is called terminates -- either normally or through an
559unhandled exception -- or until the optional timeout occurs.
560
Fred Drake12686782002-03-19 14:37:44 +0000561When the \var{timeout} argument is present and not \code{None}, it
562should be a floating point number specifying a timeout for the
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000563operation in seconds (or fractions thereof).
564
565A thread can be \method{join()}ed many times.
566
567A thread cannot join itself because this would cause a
568deadlock.
569
570It is an error to attempt to \method{join()} a thread before it has
571been started.
572\end{methoddesc}
573
Andrew M. Kuchling16440e61998-07-20 13:46:10 +0000574\begin{methoddesc}{getName}{}
575Return the thread's name.
576\end{methoddesc}
577
578\begin{methoddesc}{setName}{name}
579Set the thread's name.
580
581The name is a string used for identification purposes only.
582It has no semantics. Multiple threads may be given the same
583name. The initial name is set by the constructor.
584\end{methoddesc}
585
586\begin{methoddesc}{isAlive}{}
587Return whether the thread is alive.
588
589Roughly, a thread is alive from the moment the \method{start()} method
590returns until its \method{run()} method terminates.
591\end{methoddesc}
592
593\begin{methoddesc}{isDaemon}{}
594Return the thread's daemon flag.
595\end{methoddesc}
596
597\begin{methoddesc}{setDaemon}{daemonic}
598Set the thread's daemon flag to the Boolean value \var{daemonic}.
599This must be called before \method{start()} is called.
600
601The initial value is inherited from the creating thread.
602
603The entire Python program exits when no active non-daemon
604threads are left.
605\end{methoddesc}
Martin v. Löwis44f86962001-09-05 13:44:54 +0000606
607
608\subsection{Timer Objects \label{timer-objects}}
609
Fred Drake12686782002-03-19 14:37:44 +0000610This class represents an action that should be run only after a
611certain amount of time has passed --- a timer. \class{Timer} is a
612subclass of \class{Thread} and as such also functions as an example of
613creating custom threads.
Martin v. Löwis44f86962001-09-05 13:44:54 +0000614
Fred Drake12686782002-03-19 14:37:44 +0000615Timers are started, as with threads, by calling their \method{start()}
616method. The timer can be stopped (before its action has begun) by
617calling the \method{cancel()} method. The interval the timer will
618wait before executing its action may not be exactly the same as the
619interval specified by the user.
Martin v. Löwis44f86962001-09-05 13:44:54 +0000620
621For example:
622\begin{verbatim}
623def hello():
624 print "hello, world"
625
626t = Timer(30.0, hello)
627t.start() # after 30 seconds, "hello, world" will be printed
628\end{verbatim}
629
630\begin{classdesc}{Timer}{interval, function, args=[], kwargs=\{\}}
631Create a timer that will run \var{function} with arguments \var{args} and
632keyword arguments \var{kwargs}, after \var{interval} seconds have passed.
633\end{classdesc}
634
635\begin{methoddesc}{cancel}{}
Fred Drake12686782002-03-19 14:37:44 +0000636Stop the timer, and cancel the execution of the timer's action. This
637will only work if the timer is still in its waiting stage.
Martin v. Löwis44f86962001-09-05 13:44:54 +0000638\end{methoddesc}