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 | 9643c67 | 1998-07-27 22:06:12 +0000 | [diff] [blame] | 11 | This module is safe for use with \samp{from threading import *}. It |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 12 | defines the following functions and objects: |
| 13 | |
| 14 | \begin{funcdesc}{activeCount}{} |
| 15 | Return the number of currently active \class{Thread} objects. |
| 16 | The returned count is equal to the length of the list returned by |
| 17 | \function{enumerate()}. |
| 18 | A function that returns the number of currently active threads. |
| 19 | \end{funcdesc} |
| 20 | |
| 21 | \begin{funcdesc}{Condition}{} |
| 22 | A factory function that returns a new condition variable object. |
| 23 | A condition variable allows one or more threads to wait until they |
| 24 | are notified by another thread. |
| 25 | \end{funcdesc} |
| 26 | |
| 27 | \begin{funcdesc}{currentThread}{} |
| 28 | Return the current \class{Thread} object, corresponding to the |
| 29 | caller's thread of control. If the caller's thread of control was not |
| 30 | created through the |
| 31 | \module{threading} module, a dummy thread object with limited functionality |
| 32 | is returned. |
| 33 | \end{funcdesc} |
| 34 | |
| 35 | \begin{funcdesc}{enumerate}{} |
| 36 | Return a list of all currently active \class{Thread} objects. |
| 37 | The list includes daemonic threads, dummy thread objects created |
| 38 | by \function{currentThread()}, and the main thread. It excludes terminated |
| 39 | threads and threads that have not yet been started. |
| 40 | \end{funcdesc} |
| 41 | |
| 42 | \begin{funcdesc}{Event}{} |
| 43 | A factory function that returns a new event object. An event |
| 44 | manages a flag that can be set to true with the \method{set()} method and |
| 45 | reset to false with the \method{clear()} method. The \method{wait()} method blocks |
| 46 | until the flag is true. |
| 47 | \end{funcdesc} |
| 48 | |
| 49 | \begin{funcdesc}{Lock}{} |
| 50 | A factory function that returns a new primitive lock object. Once |
| 51 | a thread has acquired it, subsequent attempts to acquire it block, |
| 52 | until it is released; any thread may release it. |
| 53 | \end{funcdesc} |
| 54 | |
| 55 | \begin{funcdesc}{RLock}{} |
| 56 | A factory function that returns a new reentrant lock object. |
| 57 | A reentrant lock must be released by the thread that acquired it. |
| 58 | Once a thread has acquired a reentrant lock, the same thread may |
| 59 | acquire it again without blocking; the thread must release it once |
| 60 | for each time it has acquired it. |
| 61 | \end{funcdesc} |
| 62 | |
Fred Drake | 31d833d | 2001-08-20 18:49:00 +0000 | [diff] [blame^] | 63 | \begin{funcdesc}{Semaphore}{\optional{value}} |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 64 | A factory function that returns a new semaphore object. A |
| 65 | semaphore manages a counter representing the number of \method{release()} |
| 66 | calls minus the number of \method{acquire()} calls, plus an initial value. |
| 67 | The \method{acquire()} method blocks if necessary until it can return |
Fred Drake | 31d833d | 2001-08-20 18:49:00 +0000 | [diff] [blame^] | 68 | without making the counter negative. If not given, \var{value} defaults to |
| 69 | 1. |
| 70 | \end{funcdesc} |
| 71 | |
| 72 | \begin{funcdesc}{BoundedSemaphore}{\optional{value}} |
| 73 | A factory function that returns a new bounded semaphore object. A bounded |
| 74 | semaphore checks to make sure its current value doesn't exceed its initial |
| 75 | value. If it does, \exception{ValueError} is raised. In most situations |
| 76 | semaphores are used to guard resources with limited capacity. If the |
| 77 | semaphore is released too many times it's a sign of a bug. If not given, |
| 78 | \var{value} defaults to 1. |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 79 | \end{funcdesc} |
| 80 | |
Fred Drake | c19f392 | 2001-05-31 20:24:07 +0000 | [diff] [blame] | 81 | \begin{classdesc*}{Thread}{} |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 82 | A class that represents a thread of control. This class can be safely subclassed in a limited fashion. |
Fred Drake | c19f392 | 2001-05-31 20:24:07 +0000 | [diff] [blame] | 83 | \end{classdesc*} |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 84 | |
| 85 | Detailed interfaces for the objects are documented below. |
| 86 | |
| 87 | The design of this module is loosely based on Java's threading model. |
| 88 | However, where Java makes locks and condition variables basic behavior |
| 89 | of every object, they are separate objects in Python. Python's \class{Thread} |
| 90 | class supports a subset of the behavior of Java's Thread class; |
| 91 | currently, there are no priorities, no thread groups, and threads |
| 92 | cannot be destroyed, stopped, suspended, resumed, or interrupted. The |
| 93 | static methods of Java's Thread class, when implemented, are mapped to |
| 94 | module-level functions. |
| 95 | |
| 96 | All of the methods described below are executed atomically. |
| 97 | |
Fred Drake | bf5a6d2 | 1999-03-12 19:57:38 +0000 | [diff] [blame] | 98 | |
| 99 | \subsection{Lock Objects \label{lock-objects}} |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 100 | |
| 101 | A primitive lock is a synchronization primitive that is not owned |
| 102 | by a particular thread when locked. In Python, it is currently |
| 103 | the lowest level synchronization primitive available, implemented |
Fred Drake | ffbe687 | 1999-04-22 21:23:22 +0000 | [diff] [blame] | 104 | directly by the \refmodule{thread} extension module. |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 105 | |
| 106 | A primitive lock is in one of two states, ``locked'' or ``unlocked''. |
| 107 | It is created in the unlocked state. It has two basic methods, |
| 108 | \method{acquire()} and \method{release()}. When the state is |
| 109 | unlocked, \method{acquire()} changes the state to locked and returns |
| 110 | immediately. When the state is locked, \method{acquire()} blocks |
| 111 | until a call to \method{release()} in another thread changes it to |
| 112 | unlocked, then the \method{acquire()} call resets it to locked and |
| 113 | returns. The \method{release()} method should only be called in the |
| 114 | locked state; it changes the state to unlocked and returns |
| 115 | immediately. When more than one thread is blocked in |
| 116 | \method{acquire()} waiting for the state to turn to unlocked, only one |
| 117 | thread proceeds when a \method{release()} call resets the state to |
| 118 | unlocked; which one of the waiting threads proceeds is not defined, |
| 119 | and may vary across implementations. |
| 120 | |
| 121 | All methods are executed atomically. |
| 122 | |
Fred Drake | bf5a6d2 | 1999-03-12 19:57:38 +0000 | [diff] [blame] | 123 | \begin{methoddesc}{acquire}{\optional{blocking\code{ = 1}}} |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 124 | Acquire a lock, blocking or non-blocking. |
| 125 | |
| 126 | When invoked without arguments, block until the lock is |
| 127 | unlocked, then set it to locked, and return. There is no |
| 128 | return value in this case. |
| 129 | |
| 130 | When invoked with the \var{blocking} argument set to true, do the |
| 131 | same thing as when called without arguments, and return true. |
| 132 | |
| 133 | When invoked with the \var{blocking} argument set to false, do not |
| 134 | block. If a call without an argument would block, return false |
| 135 | immediately; otherwise, do the same thing as when called |
| 136 | without arguments, and return true. |
| 137 | \end{methoddesc} |
| 138 | |
| 139 | \begin{methoddesc}{release}{} |
| 140 | Release a lock. |
| 141 | |
| 142 | When the lock is locked, reset it to unlocked, and return. If |
| 143 | any other threads are blocked waiting for the lock to become |
| 144 | unlocked, allow exactly one of them to proceed. |
| 145 | |
| 146 | Do not call this method when the lock is unlocked. |
| 147 | |
| 148 | There is no return value. |
| 149 | \end{methoddesc} |
| 150 | |
Fred Drake | bf5a6d2 | 1999-03-12 19:57:38 +0000 | [diff] [blame] | 151 | |
| 152 | \subsection{RLock Objects \label{rlock-objects}} |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 153 | |
| 154 | A reentrant lock is a synchronization primitive that may be |
| 155 | acquired multiple times by the same thread. Internally, it uses |
| 156 | the concepts of ``owning thread'' and ``recursion level'' in |
| 157 | addition to the locked/unlocked state used by primitive locks. In |
| 158 | the locked state, some thread owns the lock; in the unlocked |
| 159 | state, no thread owns it. |
| 160 | |
| 161 | To lock the lock, a thread calls its \method{acquire()} method; this |
| 162 | returns once the thread owns the lock. To unlock the lock, a |
Fred Drake | 907e76b | 2001-07-06 20:30:11 +0000 | [diff] [blame] | 163 | thread calls its \method{release()} method. |
| 164 | \method{acquire()}/\method{release()} call pairs may be nested; only |
| 165 | the final \method{release()} (the \method{release()} of the outermost |
| 166 | pair) resets the lock to unlocked and allows another thread blocked in |
| 167 | \method{acquire()} to proceed. |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 168 | |
Fred Drake | bf5a6d2 | 1999-03-12 19:57:38 +0000 | [diff] [blame] | 169 | \begin{methoddesc}{acquire}{\optional{blocking\code{ = 1}}} |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 170 | Acquire a lock, blocking or non-blocking. |
| 171 | |
| 172 | When invoked without arguments: if this thread already owns |
| 173 | the lock, increment the recursion level by one, and return |
| 174 | immediately. Otherwise, if another thread owns the lock, |
| 175 | block until the lock is unlocked. Once the lock is unlocked |
| 176 | (not owned by any thread), then grab ownership, set the |
| 177 | recursion level to one, and return. If more than one thread |
| 178 | is blocked waiting until the lock is unlocked, only one at a |
| 179 | time will be able to grab ownership of the lock. There is no |
| 180 | return value in this case. |
| 181 | |
| 182 | When invoked with the \var{blocking} argument set to true, do the |
| 183 | same thing as when called without arguments, and return true. |
| 184 | |
| 185 | When invoked with the \var{blocking} argument set to false, do not |
| 186 | block. If a call without an argument would block, return false |
| 187 | immediately; otherwise, do the same thing as when called |
| 188 | without arguments, and return true. |
| 189 | \end{methoddesc} |
| 190 | |
| 191 | \begin{methoddesc}{release}{} |
| 192 | Release a lock, decrementing the recursion level. If after the |
| 193 | decrement it is zero, reset the lock to unlocked (not owned by any |
| 194 | thread), and if any other threads are blocked waiting for the lock to |
| 195 | become unlocked, allow exactly one of them to proceed. If after the |
| 196 | decrement the recursion level is still nonzero, the lock remains |
| 197 | locked and owned by the calling thread. |
| 198 | |
| 199 | Only call this method when the calling thread owns the lock. |
| 200 | Do not call this method when the lock is unlocked. |
| 201 | |
| 202 | There is no return value. |
| 203 | \end{methoddesc} |
| 204 | |
Fred Drake | bf5a6d2 | 1999-03-12 19:57:38 +0000 | [diff] [blame] | 205 | |
| 206 | \subsection{Condition Objects \label{condition-objects}} |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 207 | |
| 208 | A condition variable is always associated with some kind of lock; |
| 209 | this can be passed in or one will be created by default. (Passing |
| 210 | one in is useful when several condition variables must share the |
| 211 | same lock.) |
| 212 | |
| 213 | A condition variable has \method{acquire()} and \method{release()} |
| 214 | methods that call the corresponding methods of the associated lock. |
| 215 | It also has a \method{wait()} method, and \method{notify()} and |
| 216 | \method{notifyAll()} methods. These three must only be called when |
| 217 | the calling thread has acquired the lock. |
| 218 | |
| 219 | The \method{wait()} method releases the lock, and then blocks until it |
| 220 | is awakened by a \method{notify()} or \method{notifyAll()} call for |
| 221 | the same condition variable in another thread. Once awakened, it |
| 222 | re-acquires the lock and returns. It is also possible to specify a |
| 223 | timeout. |
| 224 | |
| 225 | The \method{notify()} method wakes up one of the threads waiting for |
| 226 | the condition variable, if any are waiting. The \method{notifyAll()} |
| 227 | method wakes up all threads waiting for the condition variable. |
| 228 | |
| 229 | Note: the \method{notify()} and \method{notifyAll()} methods don't |
| 230 | release the lock; this means that the thread or threads awakened will |
| 231 | not return from their \method{wait()} call immediately, but only when |
| 232 | the thread that called \method{notify()} or \method{notifyAll()} |
| 233 | finally relinquishes ownership of the lock. |
| 234 | |
| 235 | Tip: the typical programming style using condition variables uses the |
| 236 | lock to synchronize access to some shared state; threads that are |
| 237 | interested in a particular change of state call \method{wait()} |
| 238 | repeatedly until they see the desired state, while threads that modify |
| 239 | the state call \method{notify()} or \method{notifyAll()} when they |
| 240 | change the state in such a way that it could possibly be a desired |
| 241 | state for one of the waiters. For example, the following code is a |
| 242 | generic producer-consumer situation with unlimited buffer capacity: |
| 243 | |
| 244 | \begin{verbatim} |
| 245 | # Consume one item |
| 246 | cv.acquire() |
| 247 | while not an_item_is_available(): |
| 248 | cv.wait() |
| 249 | get_an_available_item() |
| 250 | cv.release() |
| 251 | |
| 252 | # Produce one item |
| 253 | cv.acquire() |
| 254 | make_an_item_available() |
| 255 | cv.notify() |
| 256 | cv.release() |
| 257 | \end{verbatim} |
| 258 | |
| 259 | To choose between \method{notify()} and \method{notifyAll()}, consider |
| 260 | whether one state change can be interesting for only one or several |
| 261 | waiting threads. E.g. in a typical producer-consumer situation, |
| 262 | adding one item to the buffer only needs to wake up one consumer |
| 263 | thread. |
| 264 | |
Fred Drake | bf5a6d2 | 1999-03-12 19:57:38 +0000 | [diff] [blame] | 265 | \begin{classdesc}{Condition}{\optional{lock}} |
| 266 | If 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 |
| 268 | lock. Otherwise, a new \class{RLock} object is created and used as |
| 269 | the underlying lock. |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 270 | \end{classdesc} |
| 271 | |
| 272 | \begin{methoddesc}{acquire}{*args} |
| 273 | Acquire the underlying lock. |
| 274 | This method calls the corresponding method on the underlying |
| 275 | lock; the return value is whatever that method returns. |
| 276 | \end{methoddesc} |
| 277 | |
| 278 | \begin{methoddesc}{release}{} |
| 279 | Release the underlying lock. |
| 280 | This method calls the corresponding method on the underlying |
| 281 | lock; there is no return value. |
| 282 | \end{methoddesc} |
| 283 | |
Fred Drake | bf5a6d2 | 1999-03-12 19:57:38 +0000 | [diff] [blame] | 284 | \begin{methoddesc}{wait}{\optional{timeout}} |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 285 | Wait until notified or until a timeout occurs. |
| 286 | This must only be called when the calling thread has acquired the |
| 287 | lock. |
| 288 | |
| 289 | This method releases the underlying lock, and then blocks until it is |
| 290 | awakened by a \method{notify()} or \method{notifyAll()} call for the |
| 291 | same condition variable in another thread, or until the optional |
| 292 | timeout occurs. Once awakened or timed out, it re-acquires the lock |
| 293 | and returns. |
| 294 | |
Fred Drake | bf5a6d2 | 1999-03-12 19:57:38 +0000 | [diff] [blame] | 295 | When the \var{timeout} argument is present and not \code{None}, it |
| 296 | should be a floating point number specifying a timeout for the |
| 297 | operation in seconds (or fractions thereof). |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 298 | |
Fred Drake | bf5a6d2 | 1999-03-12 19:57:38 +0000 | [diff] [blame] | 299 | When the underlying lock is an \class{RLock}, it is not released using |
| 300 | its \method{release()} method, since this may not actually unlock the |
| 301 | lock when it was acquired multiple times recursively. Instead, an |
| 302 | internal interface of the \class{RLock} class is used, which really |
| 303 | unlocks it even when it has been recursively acquired several times. |
| 304 | Another internal interface is then used to restore the recursion level |
| 305 | when the lock is reacquired. |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 306 | \end{methoddesc} |
| 307 | |
| 308 | \begin{methoddesc}{notify}{} |
| 309 | Wake up a thread waiting on this condition, if any. |
| 310 | This must only be called when the calling thread has acquired the |
| 311 | lock. |
| 312 | |
| 313 | This method wakes up one of the threads waiting for the condition |
| 314 | variable, if any are waiting; it is a no-op if no threads are waiting. |
| 315 | |
| 316 | The current implementation wakes up exactly one thread, if any are |
| 317 | waiting. However, it's not safe to rely on this behavior. A future, |
| 318 | optimized implementation may occasionally wake up more than one |
| 319 | thread. |
| 320 | |
| 321 | Note: 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}{} |
| 327 | Wake 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 Drake | bf5a6d2 | 1999-03-12 19:57:38 +0000 | [diff] [blame] | 331 | |
| 332 | \subsection{Semaphore Objects \label{semaphore-objects}} |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 333 | |
| 334 | This is one of the oldest synchronization primitives in the history of |
| 335 | computer science, invented by the early Dutch computer scientist |
Fred Drake | bf5a6d2 | 1999-03-12 19:57:38 +0000 | [diff] [blame] | 336 | Edsger W. Dijkstra (he used \method{P()} and \method{V()} instead of |
| 337 | \method{acquire()} and \method{release()}). |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 338 | |
| 339 | A semaphore manages an internal counter which is decremented by each |
| 340 | \method{acquire()} call and incremented by each \method{release()} |
| 341 | call. The counter can never go below zero; when \method{acquire()} |
| 342 | finds that it is zero, it blocks, waiting until some other thread |
| 343 | calls \method{release()}. |
| 344 | |
Fred Drake | bf5a6d2 | 1999-03-12 19:57:38 +0000 | [diff] [blame] | 345 | \begin{classdesc}{Semaphore}{\optional{value}} |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 346 | The optional argument gives the initial value for the internal |
Fred Drake | bf5a6d2 | 1999-03-12 19:57:38 +0000 | [diff] [blame] | 347 | counter; it defaults to \code{1}. |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 348 | \end{classdesc} |
| 349 | |
Fred Drake | bf5a6d2 | 1999-03-12 19:57:38 +0000 | [diff] [blame] | 350 | \begin{methoddesc}{acquire}{\optional{blocking}} |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 351 | Acquire a semaphore. |
| 352 | |
| 353 | When invoked without arguments: if the internal counter is larger than |
| 354 | zero on entry, decrement it by one and return immediately. If it is |
| 355 | zero on entry, block, waiting until some other thread has called |
| 356 | \method{release()} to make it larger than zero. This is done with |
| 357 | proper interlocking so that if multiple \method{acquire()} calls are |
| 358 | blocked, \method{release()} will wake exactly one of them up. The |
| 359 | implementation may pick one at random, so the order in which blocked |
| 360 | threads are awakened should not be relied on. There is no return |
| 361 | value in this case. |
| 362 | |
Fred Drake | bf5a6d2 | 1999-03-12 19:57:38 +0000 | [diff] [blame] | 363 | When invoked with \var{blocking} set to true, do the same thing as |
| 364 | when called without arguments, and return true. |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 365 | |
Fred Drake | bf5a6d2 | 1999-03-12 19:57:38 +0000 | [diff] [blame] | 366 | When invoked with \var{blocking} set to false, do not block. If a |
| 367 | call without an argument would block, return false immediately; |
| 368 | otherwise, do the same thing as when called without arguments, and |
| 369 | return true. |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 370 | \end{methoddesc} |
| 371 | |
| 372 | \begin{methoddesc}{release}{} |
| 373 | Release a semaphore, |
| 374 | incrementing the internal counter by one. When it was zero on |
| 375 | entry and another thread is waiting for it to become larger |
| 376 | than zero again, wake up that thread. |
| 377 | \end{methoddesc} |
| 378 | |
Fred Drake | bf5a6d2 | 1999-03-12 19:57:38 +0000 | [diff] [blame] | 379 | |
Fred Drake | 31d833d | 2001-08-20 18:49:00 +0000 | [diff] [blame^] | 380 | \subsubsection{\class{Semaphore} Example \label{semaphore-examples}} |
| 381 | |
| 382 | Semaphores are often used to guard resources with limited capacity, for |
| 383 | example, a database server. In any situation where the size of the resource |
| 384 | size is fixed, you should use a bounded semaphore. Before spawning any |
| 385 | worker threads, your main thread would initialize the semaphore: |
| 386 | |
| 387 | \begin{verbatim} |
| 388 | maxconnections = 5 |
| 389 | ... |
| 390 | pool_sema = BoundedSemaphore(value=maxconnections) |
| 391 | \end{verbatim} |
| 392 | |
| 393 | Once spawned, worker threads call the semaphore's acquire and release |
| 394 | methods when they need to connect to the server: |
| 395 | |
| 396 | \begin{verbatim} |
| 397 | pool_sema.acquire() |
| 398 | conn = connectdb() |
| 399 | ... use connection ... |
| 400 | conn.close() |
| 401 | pool_sema.release() |
| 402 | \end{verbatim} |
| 403 | |
| 404 | The use of a bounded semaphore reduces the chance that a programming error |
| 405 | which causes the semaphore to be released more than it's acquired will go |
| 406 | undetected. |
| 407 | |
Fred Drake | bf5a6d2 | 1999-03-12 19:57:38 +0000 | [diff] [blame] | 408 | \subsection{Event Objects \label{event-objects}} |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 409 | |
| 410 | This is one of the simplest mechanisms for communication between |
Fred Drake | 8611921 | 1999-04-23 20:07:02 +0000 | [diff] [blame] | 411 | threads: one thread signals an event and one or more other threads |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 412 | are waiting for it. |
| 413 | |
| 414 | An event object manages an internal flag that can be set to true with |
| 415 | the \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}{} |
| 420 | The internal flag is initially false. |
| 421 | \end{classdesc} |
| 422 | |
| 423 | \begin{methoddesc}{isSet}{} |
| 424 | Return true if and only if the internal flag is true. |
| 425 | \end{methoddesc} |
| 426 | |
| 427 | \begin{methoddesc}{set}{} |
| 428 | Set the internal flag to true. |
| 429 | All threads waiting for it to become true are awakened. |
| 430 | Threads that call \method{wait()} once the flag is true will not block |
| 431 | at all. |
| 432 | \end{methoddesc} |
| 433 | |
| 434 | \begin{methoddesc}{clear}{} |
| 435 | Reset the internal flag to false. |
| 436 | Subsequently, threads calling \method{wait()} will block until \method{set()} is |
| 437 | called to set the internal flag to true again. |
| 438 | \end{methoddesc} |
| 439 | |
Fred Drake | bf5a6d2 | 1999-03-12 19:57:38 +0000 | [diff] [blame] | 440 | \begin{methoddesc}{wait}{\optional{timeout}} |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 441 | Block until the internal flag is true. |
| 442 | If the internal flag is true on entry, return immediately. Otherwise, |
| 443 | block until another thread calls \method{set()} to set the flag to |
| 444 | true, or until the optional timeout occurs. |
| 445 | |
| 446 | When the timeout argument is present and not \code{None}, it should be a |
| 447 | floating point number specifying a timeout for the operation in |
| 448 | seconds (or fractions thereof). |
| 449 | \end{methoddesc} |
| 450 | |
Fred Drake | bf5a6d2 | 1999-03-12 19:57:38 +0000 | [diff] [blame] | 451 | |
| 452 | \subsection{Thread Objects \label{thread-objects}} |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 453 | |
| 454 | This class represents an activity that is run in a separate thread |
| 455 | of control. There are two ways to specify the activity: by |
| 456 | passing a callable object to the constructor, or by overriding the |
| 457 | \method{run()} method in a subclass. No other methods (except for the |
| 458 | constructor) should be overridden in a subclass. In other words, |
Fred Drake | bf5a6d2 | 1999-03-12 19:57:38 +0000 | [diff] [blame] | 459 | \emph{only} override the \method{__init__()} and \method{run()} |
| 460 | methods of this class. |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 461 | |
| 462 | Once a thread object is created, its activity must be started by |
Fred Drake | bf5a6d2 | 1999-03-12 19:57:38 +0000 | [diff] [blame] | 463 | calling the thread's \method{start()} method. This invokes the |
| 464 | \method{run()} method in a separate thread of control. |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 465 | |
| 466 | Once the thread's activity is started, the thread is considered |
| 467 | 'alive' and 'active' (these concepts are almost, but not quite |
| 468 | exactly, the same; their definition is intentionally somewhat |
Fred Drake | bf5a6d2 | 1999-03-12 19:57:38 +0000 | [diff] [blame] | 469 | vague). It stops being alive and active when its \method{run()} |
| 470 | method terminates -- either normally, or by raising an unhandled |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 471 | exception. The \method{isAlive()} method tests whether the thread is |
| 472 | alive. |
| 473 | |
Fred Drake | bf5a6d2 | 1999-03-12 19:57:38 +0000 | [diff] [blame] | 474 | Other threads can call a thread's \method{join()} method. This blocks |
| 475 | the calling thread until the thread whose \method{join()} method is |
| 476 | called is terminated. |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 477 | |
| 478 | 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] | 479 | set with the \method{setName()} method, and retrieved with the |
| 480 | \method{getName()} method. |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 481 | |
| 482 | A thread can be flagged as a ``daemon thread''. The significance |
| 483 | of this flag is that the entire Python program exits when only |
| 484 | daemon threads are left. The initial value is inherited from the |
Fred Drake | bf5a6d2 | 1999-03-12 19:57:38 +0000 | [diff] [blame] | 485 | creating thread. The flag can be set with the \method{setDaemon()} |
Fred Drake | c19f392 | 2001-05-31 20:24:07 +0000 | [diff] [blame] | 486 | method and retrieved with the \method{isDaemon()} method. |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 487 | |
| 488 | There is a ``main thread'' object; this corresponds to the |
| 489 | initial thread of control in the Python program. It is not a |
| 490 | daemon thread. |
| 491 | |
| 492 | There is the possibility that ``dummy thread objects'' are |
| 493 | created. These are thread objects corresponding to ``alien |
| 494 | threads''. These are threads of control started outside the |
Fred Drake | 907e76b | 2001-07-06 20:30:11 +0000 | [diff] [blame] | 495 | threading module, such as directly from C code. Dummy thread objects |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 496 | have limited functionality; they are always considered alive, |
Fred Drake | bf5a6d2 | 1999-03-12 19:57:38 +0000 | [diff] [blame] | 497 | active, and daemonic, and cannot be \method{join()}ed. They are never |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 498 | deleted, since it is impossible to detect the termination of alien |
| 499 | threads. |
| 500 | |
| 501 | |
| 502 | \begin{classdesc}{Thread}{group=None, target=None, name=None, |
Fred Drake | bf5a6d2 | 1999-03-12 19:57:38 +0000 | [diff] [blame] | 503 | args=(), kwargs=\{\}} |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 504 | This constructor should always be called with keyword |
| 505 | arguments. Arguments are: |
| 506 | |
Fred Drake | c19f392 | 2001-05-31 20:24:07 +0000 | [diff] [blame] | 507 | \var{group} should be \code{None}; reserved for future extension when |
| 508 | a \class{ThreadGroup} class is implemented. |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 509 | |
Fred Drake | c19f392 | 2001-05-31 20:24:07 +0000 | [diff] [blame] | 510 | \var{target} is the callable object to be invoked by the |
| 511 | \method{run()} method. Defaults to \code{None}, meaning nothing is |
| 512 | called. |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 513 | |
Fred Drake | c19f392 | 2001-05-31 20:24:07 +0000 | [diff] [blame] | 514 | \var{name} is the thread name. By default, a unique name is |
| 515 | constructed of the form ``Thread-\var{N}'' where \var{N} is a small |
| 516 | decimal number. |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 517 | |
Fred Drake | c19f392 | 2001-05-31 20:24:07 +0000 | [diff] [blame] | 518 | \var{args} is the argument tuple for the target invocation. Defaults |
| 519 | to \code{()}. |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 520 | |
Fred Drake | c19f392 | 2001-05-31 20:24:07 +0000 | [diff] [blame] | 521 | \var{kwargs} is a dictionary of keyword arguments for the target |
| 522 | invocation. Defaults to \code{\{\}}. |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 523 | |
| 524 | If the subclass overrides the constructor, it must make sure |
Fred Drake | bf5a6d2 | 1999-03-12 19:57:38 +0000 | [diff] [blame] | 525 | to invoke the base class constructor (\code{Thread.__init__()}) |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 526 | before doing anything else to the thread. |
| 527 | \end{classdesc} |
| 528 | |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 529 | \begin{methoddesc}{start}{} |
| 530 | Start the thread's activity. |
| 531 | |
| 532 | This must be called at most once per thread object. It |
| 533 | arranges for the object's \method{run()} method to be invoked in a |
| 534 | separate thread of control. |
| 535 | \end{methoddesc} |
| 536 | |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 537 | \begin{methoddesc}{run}{} |
| 538 | Method representing the thread's activity. |
| 539 | |
| 540 | You 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 |
| 543 | arguments taken from the \var{args} and \var{kwargs} arguments, |
| 544 | respectively. |
| 545 | \end{methoddesc} |
| 546 | |
Fred Drake | bf5a6d2 | 1999-03-12 19:57:38 +0000 | [diff] [blame] | 547 | \begin{methoddesc}{join}{\optional{timeout}} |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 548 | Wait until the thread terminates. |
| 549 | This blocks the calling thread until the thread whose \method{join()} |
| 550 | method is called terminates -- either normally or through an |
| 551 | unhandled exception -- or until the optional timeout occurs. |
| 552 | |
| 553 | When the \var{timeout} argument is present and not \code{None}, it should |
| 554 | be a floating point number specifying a timeout for the |
| 555 | operation in seconds (or fractions thereof). |
| 556 | |
| 557 | A thread can be \method{join()}ed many times. |
| 558 | |
| 559 | A thread cannot join itself because this would cause a |
| 560 | deadlock. |
| 561 | |
| 562 | It is an error to attempt to \method{join()} a thread before it has |
| 563 | been started. |
| 564 | \end{methoddesc} |
| 565 | |
Andrew M. Kuchling | 16440e6 | 1998-07-20 13:46:10 +0000 | [diff] [blame] | 566 | \begin{methoddesc}{getName}{} |
| 567 | Return the thread's name. |
| 568 | \end{methoddesc} |
| 569 | |
| 570 | \begin{methoddesc}{setName}{name} |
| 571 | Set the thread's name. |
| 572 | |
| 573 | The name is a string used for identification purposes only. |
| 574 | It has no semantics. Multiple threads may be given the same |
| 575 | name. The initial name is set by the constructor. |
| 576 | \end{methoddesc} |
| 577 | |
| 578 | \begin{methoddesc}{isAlive}{} |
| 579 | Return whether the thread is alive. |
| 580 | |
| 581 | Roughly, a thread is alive from the moment the \method{start()} method |
| 582 | returns until its \method{run()} method terminates. |
| 583 | \end{methoddesc} |
| 584 | |
| 585 | \begin{methoddesc}{isDaemon}{} |
| 586 | Return the thread's daemon flag. |
| 587 | \end{methoddesc} |
| 588 | |
| 589 | \begin{methoddesc}{setDaemon}{daemonic} |
| 590 | Set the thread's daemon flag to the Boolean value \var{daemonic}. |
| 591 | This must be called before \method{start()} is called. |
| 592 | |
| 593 | The initial value is inherited from the creating thread. |
| 594 | |
| 595 | The entire Python program exits when no active non-daemon |
| 596 | threads are left. |
| 597 | \end{methoddesc} |