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