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