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