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