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