Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 1 | \section{\module{threading} --- |
Fred Drake | bf5a6d2 | 1999-03-12 19:57:38 +0000 | [diff] [blame] | 2 | Higher-level threading interface} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 3 | |
Fred Drake | bf5a6d2 | 1999-03-12 19:57:38 +0000 | [diff] [blame] | 4 | \declaremodule{standard}{threading} |
| 5 | \modulesynopsis{Higher-level threading interface.} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 6 | |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 7 | |
Fred Drake | 9643c67 | 1998-07-27 22:06:12 +0000 | [diff] [blame] | 8 | This module constructs higher-level threading interfaces on top of the |
Fred Drake | ffbe687 | 1999-04-22 21:23:22 +0000 | [diff] [blame] | 9 | lower level \refmodule{thread} module. |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 10 | |
Fred Drake | 740f800 | 2002-12-30 23:00:36 +0000 | [diff] [blame] | 11 | The \refmodule[dummythreading]{dummy_threading} module is provided for |
| 12 | situations where \module{threading} cannot be used because |
| 13 | \refmodule{thread} is missing. |
Guido van Rossum | 2969233 | 2002-12-30 22:34:10 +0000 | [diff] [blame] | 14 | |
Fred Drake | 11f89b7 | 2003-01-06 16:38:10 +0000 | [diff] [blame] | 15 | This module defines the following functions and objects: |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 16 | |
| 17 | \begin{funcdesc}{activeCount}{} |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 18 | Return the number of \class{Thread} objects currently alive. The |
| 19 | returned count is equal to the length of the list returned by |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 20 | \function{enumerate()}. |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 21 | \end{funcdesc} |
| 22 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 23 | \begin{funcdescni}{Condition}{} |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 24 | A factory function that returns a new condition variable object. |
| 25 | A condition variable allows one or more threads to wait until they |
| 26 | are notified by another thread. |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 27 | \end{funcdescni} |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 28 | |
| 29 | \begin{funcdesc}{currentThread}{} |
| 30 | Return the current \class{Thread} object, corresponding to the |
| 31 | caller's thread of control. If the caller's thread of control was not |
| 32 | created through the |
| 33 | \module{threading} module, a dummy thread object with limited functionality |
| 34 | is returned. |
| 35 | \end{funcdesc} |
| 36 | |
| 37 | \begin{funcdesc}{enumerate}{} |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 38 | Return a list of all \class{Thread} objects currently alive. The list |
| 39 | includes daemonic threads, dummy thread objects created by |
| 40 | \function{currentThread()}, and the main thread. It excludes |
| 41 | terminated threads and threads that have not yet been started. |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 42 | \end{funcdesc} |
| 43 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 44 | \begin{funcdescni}{Event}{} |
Fred Drake | 1268678 | 2002-03-19 14:37:44 +0000 | [diff] [blame] | 45 | A factory function that returns a new event object. An event manages |
| 46 | a flag that can be set to true with the \method{set()} method and |
| 47 | reset to false with the \method{clear()} method. The \method{wait()} |
| 48 | method blocks until the flag is true. |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 49 | \end{funcdescni} |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 50 | |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 51 | \begin{classdesc*}{local}{} |
| 52 | A class that represents thread-local data. Thread-local data are data |
Andrew M. Kuchling | 872dc5c | 2004-07-17 13:35:43 +0000 | [diff] [blame] | 53 | whose values are thread specific. To manage thread-local data, just |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 54 | create an instance of \class{local} (or a subclass) and store |
| 55 | attributes on it: |
| 56 | |
| 57 | \begin{verbatim} |
Andrew M. Kuchling | 872dc5c | 2004-07-17 13:35:43 +0000 | [diff] [blame] | 58 | mydata = threading.local() |
| 59 | mydata.x = 1 |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 60 | \end{verbatim} |
| 61 | |
| 62 | The instance's values will be different for separate threads. |
| 63 | |
| 64 | For more details and extensive examples, see the documentation string |
Andrew M. Kuchling | 872dc5c | 2004-07-17 13:35:43 +0000 | [diff] [blame] | 65 | of the \module{_threading_local} module. |
Jim Fulton | d15dc06 | 2004-07-14 19:11:50 +0000 | [diff] [blame] | 66 | |
| 67 | \versionadded{2.4} |
| 68 | \end{classdesc*} |
| 69 | |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 70 | \begin{funcdesc}{Lock}{} |
| 71 | A factory function that returns a new primitive lock object. Once |
| 72 | a thread has acquired it, subsequent attempts to acquire it block, |
| 73 | until it is released; any thread may release it. |
| 74 | \end{funcdesc} |
| 75 | |
| 76 | \begin{funcdesc}{RLock}{} |
| 77 | A factory function that returns a new reentrant lock object. |
| 78 | A reentrant lock must be released by the thread that acquired it. |
| 79 | Once a thread has acquired a reentrant lock, the same thread may |
| 80 | acquire it again without blocking; the thread must release it once |
| 81 | for each time it has acquired it. |
| 82 | \end{funcdesc} |
| 83 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 84 | \begin{funcdescni}{Semaphore}{\optional{value}} |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 85 | A factory function that returns a new semaphore object. A |
| 86 | semaphore manages a counter representing the number of \method{release()} |
| 87 | calls minus the number of \method{acquire()} calls, plus an initial value. |
| 88 | The \method{acquire()} method blocks if necessary until it can return |
Fred Drake | 31d833d | 2001-08-20 18:49:00 +0000 | [diff] [blame] | 89 | without making the counter negative. If not given, \var{value} defaults to |
| 90 | 1. |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 91 | \end{funcdescni} |
Fred Drake | 31d833d | 2001-08-20 18:49:00 +0000 | [diff] [blame] | 92 | |
| 93 | \begin{funcdesc}{BoundedSemaphore}{\optional{value}} |
| 94 | A factory function that returns a new bounded semaphore object. A bounded |
| 95 | semaphore checks to make sure its current value doesn't exceed its initial |
| 96 | value. If it does, \exception{ValueError} is raised. In most situations |
| 97 | semaphores are used to guard resources with limited capacity. If the |
| 98 | semaphore is released too many times it's a sign of a bug. If not given, |
| 99 | \var{value} defaults to 1. |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 100 | \end{funcdesc} |
| 101 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 102 | \begin{classdesc*}{Thread} |
Fred Drake | 1268678 | 2002-03-19 14:37:44 +0000 | [diff] [blame] | 103 | A class that represents a thread of control. This class can be safely |
| 104 | subclassed in a limited fashion. |
Fred Drake | c19f392 | 2001-05-31 20:24:07 +0000 | [diff] [blame] | 105 | \end{classdesc*} |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 106 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 107 | \begin{classdesc*}{Timer} |
Martin v. Löwis | 44f8696 | 2001-09-05 13:44:54 +0000 | [diff] [blame] | 108 | A thread that executes a function after a specified interval has passed. |
| 109 | \end{classdesc*} |
| 110 | |
Jeremy Hylton | bfccb35 | 2003-06-29 16:58:41 +0000 | [diff] [blame] | 111 | \begin{funcdesc}{settrace}{func} |
Fred Drake | 5728815 | 2003-06-29 18:12:23 +0000 | [diff] [blame] | 112 | Set a trace function\index{trace function} for all threads started |
Jeremy Hylton | bfccb35 | 2003-06-29 16:58:41 +0000 | [diff] [blame] | 113 | from the \module{threading} module. The \var{func} will be passed to |
Fred Drake | 5728815 | 2003-06-29 18:12:23 +0000 | [diff] [blame] | 114 | \function{sys.settrace()} for each thread, before its \method{run()} |
Jeremy Hylton | bfccb35 | 2003-06-29 16:58:41 +0000 | [diff] [blame] | 115 | method is called. |
Neal Norwitz | ecc7171 | 2003-06-30 21:47:47 +0000 | [diff] [blame] | 116 | \versionadded{2.3} |
Jeremy Hylton | bfccb35 | 2003-06-29 16:58:41 +0000 | [diff] [blame] | 117 | \end{funcdesc} |
| 118 | |
| 119 | \begin{funcdesc}{setprofile}{func} |
Fred Drake | 5728815 | 2003-06-29 18:12:23 +0000 | [diff] [blame] | 120 | Set a profile function\index{profile function} for all threads started |
Jeremy Hylton | bfccb35 | 2003-06-29 16:58:41 +0000 | [diff] [blame] | 121 | from the \module{threading} module. The \var{func} will be passed to |
Fred Drake | 5728815 | 2003-06-29 18:12:23 +0000 | [diff] [blame] | 122 | \function{sys.setprofile()} for each thread, before its \method{run()} |
Jeremy Hylton | bfccb35 | 2003-06-29 16:58:41 +0000 | [diff] [blame] | 123 | method is called. |
Neal Norwitz | ecc7171 | 2003-06-30 21:47:47 +0000 | [diff] [blame] | 124 | \versionadded{2.3} |
Jeremy Hylton | bfccb35 | 2003-06-29 16:58:41 +0000 | [diff] [blame] | 125 | \end{funcdesc} |
| 126 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 127 | \begin{funcdesc}{stack_size}{\optional{size}} |
| 128 | Return the thread stack size used when creating new threads. The |
| 129 | optional \var{size} argument specifies the stack size to be used for |
| 130 | subsequently created threads, and must be 0 (use platform or |
| 131 | configured default) or a positive integer value of at least 32,768 (32kB). |
| 132 | If changing the thread stack size is unsupported, a \exception{ThreadError} |
| 133 | is raised. If the specified stack size is invalid, a \exception{ValueError} |
| 134 | is raised and the stack size is unmodified. 32kB is currently the minimum |
| 135 | supported stack size value to guarantee sufficient stack space for the |
| 136 | interpreter itself. Note that some platforms may have particular |
| 137 | restrictions on values for the stack size, such as requiring a minimum |
| 138 | stack size > 32kB or requiring allocation in multiples of the system |
| 139 | memory page size - platform documentation should be referred to for |
| 140 | more information (4kB pages are common; using multiples of 4096 for |
| 141 | the stack size is the suggested approach in the absence of more |
| 142 | specific information). |
| 143 | Availability: Windows, systems with \POSIX{} threads. |
| 144 | \versionadded{2.5} |
| 145 | \end{funcdesc} |
| 146 | |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 147 | Detailed interfaces for the objects are documented below. |
| 148 | |
| 149 | The design of this module is loosely based on Java's threading model. |
| 150 | However, where Java makes locks and condition variables basic behavior |
| 151 | of every object, they are separate objects in Python. Python's \class{Thread} |
| 152 | class supports a subset of the behavior of Java's Thread class; |
| 153 | currently, there are no priorities, no thread groups, and threads |
| 154 | cannot be destroyed, stopped, suspended, resumed, or interrupted. The |
| 155 | static methods of Java's Thread class, when implemented, are mapped to |
| 156 | module-level functions. |
| 157 | |
| 158 | All of the methods described below are executed atomically. |
| 159 | |
Fred Drake | bf5a6d2 | 1999-03-12 19:57:38 +0000 | [diff] [blame] | 160 | |
| 161 | \subsection{Lock Objects \label{lock-objects}} |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 162 | |
| 163 | A primitive lock is a synchronization primitive that is not owned |
| 164 | by a particular thread when locked. In Python, it is currently |
| 165 | the lowest level synchronization primitive available, implemented |
Fred Drake | ffbe687 | 1999-04-22 21:23:22 +0000 | [diff] [blame] | 166 | directly by the \refmodule{thread} extension module. |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 167 | |
| 168 | A primitive lock is in one of two states, ``locked'' or ``unlocked''. |
| 169 | It is created in the unlocked state. It has two basic methods, |
| 170 | \method{acquire()} and \method{release()}. When the state is |
| 171 | unlocked, \method{acquire()} changes the state to locked and returns |
| 172 | immediately. When the state is locked, \method{acquire()} blocks |
| 173 | until a call to \method{release()} in another thread changes it to |
| 174 | unlocked, then the \method{acquire()} call resets it to locked and |
| 175 | returns. The \method{release()} method should only be called in the |
| 176 | locked state; it changes the state to unlocked and returns |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 177 | immediately. If an attempt is made to release an unlocked lock, a |
| 178 | \exception{RuntimeError} will be raised. |
| 179 | |
| 180 | When more than one thread is blocked in \method{acquire()} waiting for |
| 181 | the state to turn to unlocked, only one thread proceeds when a |
| 182 | \method{release()} call resets the state to unlocked; which one of the |
| 183 | waiting threads proceeds is not defined, and may vary across |
| 184 | implementations. |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 185 | |
| 186 | All methods are executed atomically. |
| 187 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 188 | \begin{methoddesc}[Lock]{acquire}{\optional{blocking\code{ = 1}}} |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 189 | Acquire a lock, blocking or non-blocking. |
| 190 | |
| 191 | When invoked without arguments, block until the lock is |
Andrew M. Kuchling | 921879a | 2005-06-02 16:59:18 +0000 | [diff] [blame] | 192 | unlocked, then set it to locked, and return true. |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 193 | |
| 194 | When invoked with the \var{blocking} argument set to true, do the |
| 195 | same thing as when called without arguments, and return true. |
| 196 | |
| 197 | When invoked with the \var{blocking} argument set to false, do not |
| 198 | block. If a call without an argument would block, return false |
| 199 | immediately; otherwise, do the same thing as when called |
| 200 | without arguments, and return true. |
| 201 | \end{methoddesc} |
| 202 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 203 | \begin{methoddesc}[Lock]{release}{} |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 204 | Release a lock. |
| 205 | |
| 206 | When the lock is locked, reset it to unlocked, and return. If |
| 207 | any other threads are blocked waiting for the lock to become |
| 208 | unlocked, allow exactly one of them to proceed. |
| 209 | |
| 210 | Do not call this method when the lock is unlocked. |
| 211 | |
| 212 | There is no return value. |
| 213 | \end{methoddesc} |
| 214 | |
Fred Drake | bf5a6d2 | 1999-03-12 19:57:38 +0000 | [diff] [blame] | 215 | |
| 216 | \subsection{RLock Objects \label{rlock-objects}} |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 217 | |
| 218 | A reentrant lock is a synchronization primitive that may be |
| 219 | acquired multiple times by the same thread. Internally, it uses |
| 220 | the concepts of ``owning thread'' and ``recursion level'' in |
| 221 | addition to the locked/unlocked state used by primitive locks. In |
| 222 | the locked state, some thread owns the lock; in the unlocked |
| 223 | state, no thread owns it. |
| 224 | |
| 225 | To lock the lock, a thread calls its \method{acquire()} method; this |
| 226 | returns once the thread owns the lock. To unlock the lock, a |
Fred Drake | 907e76b | 2001-07-06 20:30:11 +0000 | [diff] [blame] | 227 | thread calls its \method{release()} method. |
| 228 | \method{acquire()}/\method{release()} call pairs may be nested; only |
| 229 | the final \method{release()} (the \method{release()} of the outermost |
| 230 | pair) resets the lock to unlocked and allows another thread blocked in |
| 231 | \method{acquire()} to proceed. |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 232 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 233 | \begin{methoddesc}[RLock]{acquire}{\optional{blocking\code{ = 1}}} |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 234 | Acquire a lock, blocking or non-blocking. |
| 235 | |
| 236 | When invoked without arguments: if this thread already owns |
| 237 | the lock, increment the recursion level by one, and return |
| 238 | immediately. Otherwise, if another thread owns the lock, |
| 239 | block until the lock is unlocked. Once the lock is unlocked |
| 240 | (not owned by any thread), then grab ownership, set the |
| 241 | recursion level to one, and return. If more than one thread |
| 242 | is blocked waiting until the lock is unlocked, only one at a |
| 243 | time will be able to grab ownership of the lock. There is no |
| 244 | return value in this case. |
| 245 | |
| 246 | When invoked with the \var{blocking} argument set to true, do the |
| 247 | same thing as when called without arguments, and return true. |
| 248 | |
| 249 | When invoked with the \var{blocking} argument set to false, do not |
| 250 | block. If a call without an argument would block, return false |
| 251 | immediately; otherwise, do the same thing as when called |
| 252 | without arguments, and return true. |
| 253 | \end{methoddesc} |
| 254 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 255 | \begin{methoddesc}[RLock]{release}{} |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 256 | Release a lock, decrementing the recursion level. If after the |
| 257 | decrement it is zero, reset the lock to unlocked (not owned by any |
| 258 | thread), and if any other threads are blocked waiting for the lock to |
| 259 | become unlocked, allow exactly one of them to proceed. If after the |
| 260 | decrement the recursion level is still nonzero, the lock remains |
| 261 | locked and owned by the calling thread. |
| 262 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 263 | Only call this method when the calling thread owns the lock. A |
| 264 | \exception{RuntimeError} is raised if this method is called when the |
| 265 | lock is unlocked. |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 266 | |
| 267 | There is no return value. |
| 268 | \end{methoddesc} |
| 269 | |
Fred Drake | bf5a6d2 | 1999-03-12 19:57:38 +0000 | [diff] [blame] | 270 | |
| 271 | \subsection{Condition Objects \label{condition-objects}} |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 272 | |
| 273 | A condition variable is always associated with some kind of lock; |
| 274 | this can be passed in or one will be created by default. (Passing |
| 275 | one in is useful when several condition variables must share the |
| 276 | same lock.) |
| 277 | |
| 278 | A condition variable has \method{acquire()} and \method{release()} |
| 279 | methods that call the corresponding methods of the associated lock. |
| 280 | It also has a \method{wait()} method, and \method{notify()} and |
| 281 | \method{notifyAll()} methods. These three must only be called when |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 282 | the calling thread has acquired the lock, otherwise a |
| 283 | \exception{RuntimeError} is raised. |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 284 | |
| 285 | The \method{wait()} method releases the lock, and then blocks until it |
| 286 | is awakened by a \method{notify()} or \method{notifyAll()} call for |
| 287 | the same condition variable in another thread. Once awakened, it |
| 288 | re-acquires the lock and returns. It is also possible to specify a |
| 289 | timeout. |
| 290 | |
| 291 | The \method{notify()} method wakes up one of the threads waiting for |
| 292 | the condition variable, if any are waiting. The \method{notifyAll()} |
| 293 | method wakes up all threads waiting for the condition variable. |
| 294 | |
| 295 | Note: the \method{notify()} and \method{notifyAll()} methods don't |
| 296 | release the lock; this means that the thread or threads awakened will |
| 297 | not return from their \method{wait()} call immediately, but only when |
| 298 | the thread that called \method{notify()} or \method{notifyAll()} |
| 299 | finally relinquishes ownership of the lock. |
| 300 | |
| 301 | Tip: the typical programming style using condition variables uses the |
| 302 | lock to synchronize access to some shared state; threads that are |
| 303 | interested in a particular change of state call \method{wait()} |
| 304 | repeatedly until they see the desired state, while threads that modify |
| 305 | the state call \method{notify()} or \method{notifyAll()} when they |
| 306 | change the state in such a way that it could possibly be a desired |
| 307 | state for one of the waiters. For example, the following code is a |
| 308 | generic producer-consumer situation with unlimited buffer capacity: |
| 309 | |
| 310 | \begin{verbatim} |
| 311 | # Consume one item |
| 312 | cv.acquire() |
| 313 | while not an_item_is_available(): |
| 314 | cv.wait() |
| 315 | get_an_available_item() |
| 316 | cv.release() |
| 317 | |
| 318 | # Produce one item |
| 319 | cv.acquire() |
| 320 | make_an_item_available() |
| 321 | cv.notify() |
| 322 | cv.release() |
| 323 | \end{verbatim} |
| 324 | |
| 325 | To choose between \method{notify()} and \method{notifyAll()}, consider |
| 326 | whether one state change can be interesting for only one or several |
| 327 | waiting threads. E.g. in a typical producer-consumer situation, |
| 328 | adding one item to the buffer only needs to wake up one consumer |
| 329 | thread. |
| 330 | |
Fred Drake | bf5a6d2 | 1999-03-12 19:57:38 +0000 | [diff] [blame] | 331 | \begin{classdesc}{Condition}{\optional{lock}} |
| 332 | If the \var{lock} argument is given and not \code{None}, it must be a |
| 333 | \class{Lock} or \class{RLock} object, and it is used as the underlying |
| 334 | lock. Otherwise, a new \class{RLock} object is created and used as |
| 335 | the underlying lock. |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 336 | \end{classdesc} |
| 337 | |
| 338 | \begin{methoddesc}{acquire}{*args} |
| 339 | Acquire the underlying lock. |
| 340 | This method calls the corresponding method on the underlying |
| 341 | lock; the return value is whatever that method returns. |
| 342 | \end{methoddesc} |
| 343 | |
| 344 | \begin{methoddesc}{release}{} |
| 345 | Release the underlying lock. |
| 346 | This method calls the corresponding method on the underlying |
| 347 | lock; there is no return value. |
| 348 | \end{methoddesc} |
| 349 | |
Fred Drake | bf5a6d2 | 1999-03-12 19:57:38 +0000 | [diff] [blame] | 350 | \begin{methoddesc}{wait}{\optional{timeout}} |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 351 | Wait until notified or until a timeout occurs. If the calling thread |
| 352 | has not acquired the lock when this method is called, a |
| 353 | \exception{RuntimeError} is raised. |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 354 | |
| 355 | This method releases the underlying lock, and then blocks until it is |
| 356 | awakened by a \method{notify()} or \method{notifyAll()} call for the |
| 357 | same condition variable in another thread, or until the optional |
| 358 | timeout occurs. Once awakened or timed out, it re-acquires the lock |
| 359 | and returns. |
| 360 | |
Fred Drake | bf5a6d2 | 1999-03-12 19:57:38 +0000 | [diff] [blame] | 361 | When the \var{timeout} argument is present and not \code{None}, it |
| 362 | should be a floating point number specifying a timeout for the |
| 363 | operation in seconds (or fractions thereof). |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 364 | |
Fred Drake | bf5a6d2 | 1999-03-12 19:57:38 +0000 | [diff] [blame] | 365 | When the underlying lock is an \class{RLock}, it is not released using |
| 366 | its \method{release()} method, since this may not actually unlock the |
| 367 | lock when it was acquired multiple times recursively. Instead, an |
| 368 | internal interface of the \class{RLock} class is used, which really |
| 369 | unlocks it even when it has been recursively acquired several times. |
| 370 | Another internal interface is then used to restore the recursion level |
| 371 | when the lock is reacquired. |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 372 | \end{methoddesc} |
| 373 | |
| 374 | \begin{methoddesc}{notify}{} |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 375 | Wake up a thread waiting on this condition, if any. Wait until |
| 376 | notified or until a timeout occurs. If the calling thread has not |
| 377 | acquired the lock when this method is called, a |
| 378 | \exception{RuntimeError} is raised. |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 379 | |
| 380 | This method wakes up one of the threads waiting for the condition |
| 381 | variable, if any are waiting; it is a no-op if no threads are waiting. |
| 382 | |
| 383 | The current implementation wakes up exactly one thread, if any are |
| 384 | waiting. However, it's not safe to rely on this behavior. A future, |
| 385 | optimized implementation may occasionally wake up more than one |
| 386 | thread. |
| 387 | |
| 388 | Note: the awakened thread does not actually return from its |
| 389 | \method{wait()} call until it can reacquire the lock. Since |
| 390 | \method{notify()} does not release the lock, its caller should. |
| 391 | \end{methoddesc} |
| 392 | |
| 393 | \begin{methoddesc}{notifyAll}{} |
| 394 | Wake up all threads waiting on this condition. This method acts like |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 395 | \method{notify()}, but wakes up all waiting threads instead of one. If |
| 396 | the calling thread has not acquired the lock when this method is |
| 397 | called, a \exception{RuntimeError} is raised. |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 398 | \end{methoddesc} |
| 399 | |
Fred Drake | bf5a6d2 | 1999-03-12 19:57:38 +0000 | [diff] [blame] | 400 | |
| 401 | \subsection{Semaphore Objects \label{semaphore-objects}} |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 402 | |
| 403 | This is one of the oldest synchronization primitives in the history of |
| 404 | computer science, invented by the early Dutch computer scientist |
Fred Drake | bf5a6d2 | 1999-03-12 19:57:38 +0000 | [diff] [blame] | 405 | Edsger W. Dijkstra (he used \method{P()} and \method{V()} instead of |
| 406 | \method{acquire()} and \method{release()}). |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 407 | |
| 408 | A semaphore manages an internal counter which is decremented by each |
| 409 | \method{acquire()} call and incremented by each \method{release()} |
| 410 | call. The counter can never go below zero; when \method{acquire()} |
| 411 | finds that it is zero, it blocks, waiting until some other thread |
| 412 | calls \method{release()}. |
| 413 | |
Fred Drake | bf5a6d2 | 1999-03-12 19:57:38 +0000 | [diff] [blame] | 414 | \begin{classdesc}{Semaphore}{\optional{value}} |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 415 | The optional argument gives the initial \var{value} for the internal |
| 416 | counter; it defaults to \code{1}. If the \var{value} given is less |
| 417 | than 0, \exception{ValueError} is raised. |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 418 | \end{classdesc} |
| 419 | |
Fred Drake | bf5a6d2 | 1999-03-12 19:57:38 +0000 | [diff] [blame] | 420 | \begin{methoddesc}{acquire}{\optional{blocking}} |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 421 | Acquire a semaphore. |
| 422 | |
| 423 | When invoked without arguments: if the internal counter is larger than |
| 424 | zero on entry, decrement it by one and return immediately. If it is |
| 425 | zero on entry, block, waiting until some other thread has called |
| 426 | \method{release()} to make it larger than zero. This is done with |
| 427 | proper interlocking so that if multiple \method{acquire()} calls are |
| 428 | blocked, \method{release()} will wake exactly one of them up. The |
| 429 | implementation may pick one at random, so the order in which blocked |
| 430 | threads are awakened should not be relied on. There is no return |
| 431 | value in this case. |
| 432 | |
Fred Drake | bf5a6d2 | 1999-03-12 19:57:38 +0000 | [diff] [blame] | 433 | When invoked with \var{blocking} set to true, do the same thing as |
| 434 | when called without arguments, and return true. |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 435 | |
Fred Drake | bf5a6d2 | 1999-03-12 19:57:38 +0000 | [diff] [blame] | 436 | When invoked with \var{blocking} set to false, do not block. If a |
| 437 | call without an argument would block, return false immediately; |
| 438 | otherwise, do the same thing as when called without arguments, and |
| 439 | return true. |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 440 | \end{methoddesc} |
| 441 | |
| 442 | \begin{methoddesc}{release}{} |
| 443 | Release a semaphore, |
| 444 | incrementing the internal counter by one. When it was zero on |
| 445 | entry and another thread is waiting for it to become larger |
| 446 | than zero again, wake up that thread. |
| 447 | \end{methoddesc} |
| 448 | |
Fred Drake | bf5a6d2 | 1999-03-12 19:57:38 +0000 | [diff] [blame] | 449 | |
Fred Drake | 31d833d | 2001-08-20 18:49:00 +0000 | [diff] [blame] | 450 | \subsubsection{\class{Semaphore} Example \label{semaphore-examples}} |
| 451 | |
| 452 | Semaphores are often used to guard resources with limited capacity, for |
| 453 | example, a database server. In any situation where the size of the resource |
| 454 | size is fixed, you should use a bounded semaphore. Before spawning any |
| 455 | worker threads, your main thread would initialize the semaphore: |
| 456 | |
| 457 | \begin{verbatim} |
| 458 | maxconnections = 5 |
| 459 | ... |
| 460 | pool_sema = BoundedSemaphore(value=maxconnections) |
| 461 | \end{verbatim} |
| 462 | |
| 463 | Once spawned, worker threads call the semaphore's acquire and release |
| 464 | methods when they need to connect to the server: |
| 465 | |
| 466 | \begin{verbatim} |
| 467 | pool_sema.acquire() |
| 468 | conn = connectdb() |
| 469 | ... use connection ... |
| 470 | conn.close() |
| 471 | pool_sema.release() |
| 472 | \end{verbatim} |
| 473 | |
| 474 | The use of a bounded semaphore reduces the chance that a programming error |
| 475 | which causes the semaphore to be released more than it's acquired will go |
| 476 | undetected. |
| 477 | |
Fred Drake | 1268678 | 2002-03-19 14:37:44 +0000 | [diff] [blame] | 478 | |
Fred Drake | bf5a6d2 | 1999-03-12 19:57:38 +0000 | [diff] [blame] | 479 | \subsection{Event Objects \label{event-objects}} |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 480 | |
| 481 | This is one of the simplest mechanisms for communication between |
Fred Drake | 1268678 | 2002-03-19 14:37:44 +0000 | [diff] [blame] | 482 | threads: one thread signals an event and other threads wait for it. |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 483 | |
| 484 | An event object manages an internal flag that can be set to true with |
Fred Drake | 1268678 | 2002-03-19 14:37:44 +0000 | [diff] [blame] | 485 | the \method{set()} method and reset to false with the \method{clear()} |
| 486 | method. The \method{wait()} method blocks until the flag is true. |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 487 | |
| 488 | |
| 489 | \begin{classdesc}{Event}{} |
| 490 | The internal flag is initially false. |
| 491 | \end{classdesc} |
| 492 | |
| 493 | \begin{methoddesc}{isSet}{} |
| 494 | Return true if and only if the internal flag is true. |
| 495 | \end{methoddesc} |
| 496 | |
| 497 | \begin{methoddesc}{set}{} |
| 498 | Set the internal flag to true. |
| 499 | All threads waiting for it to become true are awakened. |
| 500 | Threads that call \method{wait()} once the flag is true will not block |
| 501 | at all. |
| 502 | \end{methoddesc} |
| 503 | |
| 504 | \begin{methoddesc}{clear}{} |
| 505 | Reset the internal flag to false. |
Fred Drake | 1268678 | 2002-03-19 14:37:44 +0000 | [diff] [blame] | 506 | Subsequently, threads calling \method{wait()} will block until |
| 507 | \method{set()} is called to set the internal flag to true again. |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 508 | \end{methoddesc} |
| 509 | |
Fred Drake | bf5a6d2 | 1999-03-12 19:57:38 +0000 | [diff] [blame] | 510 | \begin{methoddesc}{wait}{\optional{timeout}} |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 511 | Block until the internal flag is true. |
| 512 | If the internal flag is true on entry, return immediately. Otherwise, |
| 513 | block until another thread calls \method{set()} to set the flag to |
| 514 | true, or until the optional timeout occurs. |
| 515 | |
| 516 | When the timeout argument is present and not \code{None}, it should be a |
| 517 | floating point number specifying a timeout for the operation in |
| 518 | seconds (or fractions thereof). |
| 519 | \end{methoddesc} |
| 520 | |
Fred Drake | bf5a6d2 | 1999-03-12 19:57:38 +0000 | [diff] [blame] | 521 | |
| 522 | \subsection{Thread Objects \label{thread-objects}} |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 523 | |
| 524 | This class represents an activity that is run in a separate thread |
| 525 | of control. There are two ways to specify the activity: by |
| 526 | passing a callable object to the constructor, or by overriding the |
| 527 | \method{run()} method in a subclass. No other methods (except for the |
| 528 | constructor) should be overridden in a subclass. In other words, |
Fred Drake | bf5a6d2 | 1999-03-12 19:57:38 +0000 | [diff] [blame] | 529 | \emph{only} override the \method{__init__()} and \method{run()} |
| 530 | methods of this class. |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 531 | |
| 532 | Once a thread object is created, its activity must be started by |
Fred Drake | bf5a6d2 | 1999-03-12 19:57:38 +0000 | [diff] [blame] | 533 | calling the thread's \method{start()} method. This invokes the |
| 534 | \method{run()} method in a separate thread of control. |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 535 | |
| 536 | Once the thread's activity is started, the thread is considered |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 537 | 'alive'. It stops being alive when its \method{run()} method terminates |
| 538 | -- either normally, or by raising an unhandled exception. The |
| 539 | \method{isAlive()} method tests whether the thread is alive. |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 540 | |
Fred Drake | bf5a6d2 | 1999-03-12 19:57:38 +0000 | [diff] [blame] | 541 | Other threads can call a thread's \method{join()} method. This blocks |
| 542 | the calling thread until the thread whose \method{join()} method is |
| 543 | called is terminated. |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 544 | |
| 545 | A thread has a name. The name can be passed to the constructor, |
Fred Drake | bf5a6d2 | 1999-03-12 19:57:38 +0000 | [diff] [blame] | 546 | set with the \method{setName()} method, and retrieved with the |
| 547 | \method{getName()} method. |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 548 | |
| 549 | A thread can be flagged as a ``daemon thread''. The significance |
| 550 | of this flag is that the entire Python program exits when only |
| 551 | daemon threads are left. The initial value is inherited from the |
Fred Drake | bf5a6d2 | 1999-03-12 19:57:38 +0000 | [diff] [blame] | 552 | creating thread. The flag can be set with the \method{setDaemon()} |
Fred Drake | c19f392 | 2001-05-31 20:24:07 +0000 | [diff] [blame] | 553 | method and retrieved with the \method{isDaemon()} method. |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 554 | |
| 555 | There is a ``main thread'' object; this corresponds to the |
| 556 | initial thread of control in the Python program. It is not a |
| 557 | daemon thread. |
| 558 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 559 | There is the possibility that ``dummy thread objects'' are created. |
| 560 | These are thread objects corresponding to ``alien threads'', which |
| 561 | are threads of control started outside the threading module, such as |
| 562 | directly from C code. Dummy thread objects have limited |
| 563 | functionality; they are always considered alive and daemonic, and |
| 564 | cannot be \method{join()}ed. They are never deleted, since it is |
| 565 | impossible to detect the termination of alien threads. |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 566 | |
| 567 | |
| 568 | \begin{classdesc}{Thread}{group=None, target=None, name=None, |
Fred Drake | bf5a6d2 | 1999-03-12 19:57:38 +0000 | [diff] [blame] | 569 | args=(), kwargs=\{\}} |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 570 | This constructor should always be called with keyword |
| 571 | arguments. Arguments are: |
| 572 | |
Fred Drake | c19f392 | 2001-05-31 20:24:07 +0000 | [diff] [blame] | 573 | \var{group} should be \code{None}; reserved for future extension when |
| 574 | a \class{ThreadGroup} class is implemented. |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 575 | |
Fred Drake | c19f392 | 2001-05-31 20:24:07 +0000 | [diff] [blame] | 576 | \var{target} is the callable object to be invoked by the |
| 577 | \method{run()} method. Defaults to \code{None}, meaning nothing is |
| 578 | called. |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 579 | |
Fred Drake | c19f392 | 2001-05-31 20:24:07 +0000 | [diff] [blame] | 580 | \var{name} is the thread name. By default, a unique name is |
| 581 | constructed of the form ``Thread-\var{N}'' where \var{N} is a small |
| 582 | decimal number. |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 583 | |
Fred Drake | c19f392 | 2001-05-31 20:24:07 +0000 | [diff] [blame] | 584 | \var{args} is the argument tuple for the target invocation. Defaults |
| 585 | to \code{()}. |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 586 | |
Fred Drake | c19f392 | 2001-05-31 20:24:07 +0000 | [diff] [blame] | 587 | \var{kwargs} is a dictionary of keyword arguments for the target |
| 588 | invocation. Defaults to \code{\{\}}. |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 589 | |
| 590 | If the subclass overrides the constructor, it must make sure |
Fred Drake | bf5a6d2 | 1999-03-12 19:57:38 +0000 | [diff] [blame] | 591 | to invoke the base class constructor (\code{Thread.__init__()}) |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 592 | before doing anything else to the thread. |
| 593 | \end{classdesc} |
| 594 | |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 595 | \begin{methoddesc}{start}{} |
| 596 | Start the thread's activity. |
| 597 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 598 | It must be called at most once per thread object. It arranges for the |
| 599 | object's \method{run()} method to be invoked in a separate thread of |
| 600 | control. |
| 601 | |
| 602 | This method will raise a \exception{RuntimeException} if called more |
| 603 | than once on the same thread object. |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 604 | \end{methoddesc} |
| 605 | |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 606 | \begin{methoddesc}{run}{} |
| 607 | Method representing the thread's activity. |
| 608 | |
| 609 | You may override this method in a subclass. The standard |
Fred Drake | 1268678 | 2002-03-19 14:37:44 +0000 | [diff] [blame] | 610 | \method{run()} method invokes the callable object passed to the |
| 611 | object's constructor as the \var{target} argument, if any, with |
| 612 | sequential and keyword arguments taken from the \var{args} and |
| 613 | \var{kwargs} arguments, respectively. |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 614 | \end{methoddesc} |
| 615 | |
Fred Drake | bf5a6d2 | 1999-03-12 19:57:38 +0000 | [diff] [blame] | 616 | \begin{methoddesc}{join}{\optional{timeout}} |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 617 | Wait until the thread terminates. |
| 618 | This blocks the calling thread until the thread whose \method{join()} |
| 619 | method is called terminates -- either normally or through an |
| 620 | unhandled exception -- or until the optional timeout occurs. |
| 621 | |
Fred Drake | 1268678 | 2002-03-19 14:37:44 +0000 | [diff] [blame] | 622 | When the \var{timeout} argument is present and not \code{None}, it |
| 623 | should be a floating point number specifying a timeout for the |
Georg Brandl | 75d5106 | 2005-07-17 21:00:26 +0000 | [diff] [blame] | 624 | operation in seconds (or fractions thereof). As \method{join()} always |
| 625 | returns \code{None}, you must call \method{isAlive()} to decide whether |
| 626 | a timeout happened. |
| 627 | |
| 628 | When the \var{timeout} argument is not present or \code{None}, the |
| 629 | operation will block until the thread terminates. |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 630 | |
| 631 | A thread can be \method{join()}ed many times. |
| 632 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 633 | \method{join()} may throw a \exception{RuntimeError}, if an attempt is |
| 634 | made to join the current thread as that would cause a deadlock. It is |
| 635 | also an error to \method{join()} a thread before it has been started |
| 636 | and attempts to do so raises same exception. |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 637 | \end{methoddesc} |
| 638 | |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 639 | \begin{methoddesc}{getName}{} |
| 640 | Return the thread's name. |
| 641 | \end{methoddesc} |
| 642 | |
| 643 | \begin{methoddesc}{setName}{name} |
| 644 | Set the thread's name. |
| 645 | |
| 646 | The name is a string used for identification purposes only. |
| 647 | It has no semantics. Multiple threads may be given the same |
| 648 | name. The initial name is set by the constructor. |
| 649 | \end{methoddesc} |
| 650 | |
| 651 | \begin{methoddesc}{isAlive}{} |
| 652 | Return whether the thread is alive. |
| 653 | |
| 654 | Roughly, a thread is alive from the moment the \method{start()} method |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 655 | returns until its \method{run()} method terminates. The module |
| 656 | function \function{enumerate()} returns a list of all alive threads. |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 657 | \end{methoddesc} |
| 658 | |
| 659 | \begin{methoddesc}{isDaemon}{} |
| 660 | Return the thread's daemon flag. |
| 661 | \end{methoddesc} |
| 662 | |
| 663 | \begin{methoddesc}{setDaemon}{daemonic} |
| 664 | Set the thread's daemon flag to the Boolean value \var{daemonic}. |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 665 | This must be called before \method{start()} is called, otherwise |
| 666 | \exception{RuntimeError} is raised. |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 667 | |
| 668 | The initial value is inherited from the creating thread. |
| 669 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 670 | The entire Python program exits when no alive non-daemon threads are |
| 671 | left. |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 672 | \end{methoddesc} |
Martin v. Löwis | 44f8696 | 2001-09-05 13:44:54 +0000 | [diff] [blame] | 673 | |
| 674 | |
| 675 | \subsection{Timer Objects \label{timer-objects}} |
| 676 | |
Fred Drake | 1268678 | 2002-03-19 14:37:44 +0000 | [diff] [blame] | 677 | This class represents an action that should be run only after a |
| 678 | certain amount of time has passed --- a timer. \class{Timer} is a |
| 679 | subclass of \class{Thread} and as such also functions as an example of |
| 680 | creating custom threads. |
Martin v. Löwis | 44f8696 | 2001-09-05 13:44:54 +0000 | [diff] [blame] | 681 | |
Fred Drake | 1268678 | 2002-03-19 14:37:44 +0000 | [diff] [blame] | 682 | Timers are started, as with threads, by calling their \method{start()} |
| 683 | method. The timer can be stopped (before its action has begun) by |
| 684 | calling the \method{cancel()} method. The interval the timer will |
| 685 | wait before executing its action may not be exactly the same as the |
| 686 | interval specified by the user. |
Martin v. Löwis | 44f8696 | 2001-09-05 13:44:54 +0000 | [diff] [blame] | 687 | |
| 688 | For example: |
| 689 | \begin{verbatim} |
| 690 | def hello(): |
| 691 | print "hello, world" |
| 692 | |
| 693 | t = Timer(30.0, hello) |
| 694 | t.start() # after 30 seconds, "hello, world" will be printed |
| 695 | \end{verbatim} |
| 696 | |
| 697 | \begin{classdesc}{Timer}{interval, function, args=[], kwargs=\{\}} |
| 698 | Create a timer that will run \var{function} with arguments \var{args} and |
| 699 | keyword arguments \var{kwargs}, after \var{interval} seconds have passed. |
| 700 | \end{classdesc} |
| 701 | |
| 702 | \begin{methoddesc}{cancel}{} |
Fred Drake | 1268678 | 2002-03-19 14:37:44 +0000 | [diff] [blame] | 703 | Stop the timer, and cancel the execution of the timer's action. This |
| 704 | will only work if the timer is still in its waiting stage. |
Martin v. Löwis | 44f8696 | 2001-09-05 13:44:54 +0000 | [diff] [blame] | 705 | \end{methoddesc} |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 706 | |
| 707 | \subsection{Using locks, conditions, and semaphores in the \keyword{with} |
| 708 | statement \label{with-locks}} |
| 709 | |
| 710 | All of the objects provided by this module that have \method{acquire()} and |
| 711 | \method{release()} methods can be used as context managers for a \keyword{with} |
| 712 | statement. The \method{acquire()} method will be called when the block is |
| 713 | entered, and \method{release()} will be called when the block is exited. |
| 714 | |
| 715 | Currently, \class{Lock}, \class{RLock}, \class{Condition}, \class{Semaphore}, |
| 716 | and \class{BoundedSemaphore} objects may be used as \keyword{with} |
| 717 | statement context managers. For example: |
| 718 | |
| 719 | \begin{verbatim} |
| 720 | from __future__ import with_statement |
| 721 | import threading |
| 722 | |
| 723 | some_rlock = threading.RLock() |
| 724 | |
| 725 | with some_rlock: |
| 726 | print "some_rlock is locked while this executes" |
| 727 | \end{verbatim} |
| 728 | |