R David Murray | 6a14381 | 2013-12-20 14:37:39 -0500 | [diff] [blame] | 1 | .. currentmodule:: asyncio |
Victor Stinner | 606ab03 | 2014-02-01 03:18:58 +0100 | [diff] [blame] | 2 | .. _asyncio-sync: |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 3 | |
| 4 | Synchronization primitives |
| 5 | ========================== |
| 6 | |
lf | 627d2c8 | 2017-07-25 17:03:51 -0600 | [diff] [blame] | 7 | **Source code:** :source:`Lib/asyncio/locks.py` |
| 8 | |
Victor Stinner | 7a55b88 | 2015-01-30 00:37:04 +0100 | [diff] [blame] | 9 | Locks: |
| 10 | |
| 11 | * :class:`Lock` |
| 12 | * :class:`Event` |
| 13 | * :class:`Condition` |
Victor Stinner | 615a58e | 2015-02-25 13:55:43 +0100 | [diff] [blame] | 14 | |
| 15 | Semaphores: |
| 16 | |
Victor Stinner | 7a55b88 | 2015-01-30 00:37:04 +0100 | [diff] [blame] | 17 | * :class:`Semaphore` |
| 18 | * :class:`BoundedSemaphore` |
| 19 | |
Victor Stinner | 615a58e | 2015-02-25 13:55:43 +0100 | [diff] [blame] | 20 | asyncio lock API was designed to be close to classes of the :mod:`threading` |
| 21 | module (:class:`~threading.Lock`, :class:`~threading.Event`, |
Victor Stinner | 7a55b88 | 2015-01-30 00:37:04 +0100 | [diff] [blame] | 22 | :class:`~threading.Condition`, :class:`~threading.Semaphore`, |
Victor Stinner | 615a58e | 2015-02-25 13:55:43 +0100 | [diff] [blame] | 23 | :class:`~threading.BoundedSemaphore`), but it has no *timeout* parameter. The |
Victor Stinner | 7a55b88 | 2015-01-30 00:37:04 +0100 | [diff] [blame] | 24 | :func:`asyncio.wait_for` function can be used to cancel a task after a timeout. |
| 25 | |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 26 | |
Victor Stinner | 8c462c5 | 2014-01-24 18:11:43 +0100 | [diff] [blame] | 27 | Lock |
Andrew Svetlov | 28d8d14 | 2017-12-09 20:00:05 +0200 | [diff] [blame^] | 28 | ---- |
Victor Stinner | 8c462c5 | 2014-01-24 18:11:43 +0100 | [diff] [blame] | 29 | |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 30 | .. class:: Lock(\*, loop=None) |
| 31 | |
| 32 | Primitive lock objects. |
| 33 | |
| 34 | A primitive lock is a synchronization primitive that is not owned by a |
| 35 | particular coroutine when locked. A primitive lock is in one of two states, |
| 36 | 'locked' or 'unlocked'. |
| 37 | |
Andrew Svetlov | 28d8d14 | 2017-12-09 20:00:05 +0200 | [diff] [blame^] | 38 | The lock is created in the unlocked state. |
| 39 | It has two basic methods, :meth:`acquire` and :meth:`release`. |
| 40 | When the state is unlocked, acquire() changes the state to |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 41 | locked and returns immediately. When the state is locked, acquire() blocks |
| 42 | until a call to release() in another coroutine changes it to unlocked, then |
| 43 | the acquire() call resets it to locked and returns. The release() method |
| 44 | should only be called in the locked state; it changes the state to unlocked |
| 45 | and returns immediately. If an attempt is made to release an unlocked lock, |
| 46 | a :exc:`RuntimeError` will be raised. |
| 47 | |
| 48 | When more than one coroutine is blocked in acquire() waiting for the state |
| 49 | to turn to unlocked, only one coroutine proceeds when a release() call |
| 50 | resets the state to unlocked; first coroutine which is blocked in acquire() |
| 51 | is being processed. |
| 52 | |
Andrew Svetlov | 28d8d14 | 2017-12-09 20:00:05 +0200 | [diff] [blame^] | 53 | :meth:`acquire` is a coroutine and should be called with ``await``. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 54 | |
Andrew Svetlov | 28d8d14 | 2017-12-09 20:00:05 +0200 | [diff] [blame^] | 55 | Locks support the :ref:`context management protocol <async-with-locks>`. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 56 | |
Victor Stinner | 7b2c3c6 | 2015-02-26 10:39:16 +0100 | [diff] [blame] | 57 | This class is :ref:`not thread safe <asyncio-multithreading>`. |
| 58 | |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 59 | .. method:: locked() |
| 60 | |
Larry Hastings | 3732ed2 | 2014-03-15 21:13:56 -0700 | [diff] [blame] | 61 | Return ``True`` if the lock is acquired. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 62 | |
Victor Stinner | bdd574d | 2015-02-12 22:49:18 +0100 | [diff] [blame] | 63 | .. coroutinemethod:: acquire() |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 64 | |
| 65 | Acquire a lock. |
| 66 | |
| 67 | This method blocks until the lock is unlocked, then sets it to locked and |
| 68 | returns ``True``. |
| 69 | |
Larry Hastings | 3732ed2 | 2014-03-15 21:13:56 -0700 | [diff] [blame] | 70 | This method is a :ref:`coroutine <coroutine>`. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 71 | |
| 72 | .. method:: release() |
| 73 | |
| 74 | Release a lock. |
| 75 | |
| 76 | When the lock is locked, reset it to unlocked, and return. If any other |
| 77 | coroutines are blocked waiting for the lock to become unlocked, allow |
| 78 | exactly one of them to proceed. |
| 79 | |
| 80 | When invoked on an unlocked lock, a :exc:`RuntimeError` is raised. |
| 81 | |
| 82 | There is no return value. |
| 83 | |
| 84 | |
Victor Stinner | 8c462c5 | 2014-01-24 18:11:43 +0100 | [diff] [blame] | 85 | Event |
Andrew Svetlov | 28d8d14 | 2017-12-09 20:00:05 +0200 | [diff] [blame^] | 86 | ----- |
Victor Stinner | 8c462c5 | 2014-01-24 18:11:43 +0100 | [diff] [blame] | 87 | |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 88 | .. class:: Event(\*, loop=None) |
| 89 | |
| 90 | An Event implementation, asynchronous equivalent to :class:`threading.Event`. |
| 91 | |
| 92 | Class implementing event objects. An event manages a flag that can be set to |
| 93 | true with the :meth:`set` method and reset to false with the :meth:`clear` |
| 94 | method. The :meth:`wait` method blocks until the flag is true. The flag is |
| 95 | initially false. |
| 96 | |
Victor Stinner | 7b2c3c6 | 2015-02-26 10:39:16 +0100 | [diff] [blame] | 97 | This class is :ref:`not thread safe <asyncio-multithreading>`. |
| 98 | |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 99 | .. method:: clear() |
| 100 | |
| 101 | Reset the internal flag to false. Subsequently, coroutines calling |
| 102 | :meth:`wait` will block until :meth:`set` is called to set the internal |
| 103 | flag to true again. |
| 104 | |
| 105 | .. method:: is_set() |
| 106 | |
| 107 | Return ``True`` if and only if the internal flag is true. |
| 108 | |
| 109 | .. method:: set() |
| 110 | |
| 111 | Set the internal flag to true. All coroutines waiting for it to become |
| 112 | true are awakened. Coroutine that call :meth:`wait` once the flag is true |
| 113 | will not block at all. |
| 114 | |
Victor Stinner | bdd574d | 2015-02-12 22:49:18 +0100 | [diff] [blame] | 115 | .. coroutinemethod:: wait() |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 116 | |
| 117 | Block until the internal flag is true. |
| 118 | |
| 119 | If the internal flag is true on entry, return ``True`` immediately. |
| 120 | Otherwise, block until another coroutine calls :meth:`set` to set the |
| 121 | flag to true, then return ``True``. |
| 122 | |
Yury Selivanov | 37f15bc | 2014-02-20 16:20:44 -0500 | [diff] [blame] | 123 | This method is a :ref:`coroutine <coroutine>`. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 124 | |
| 125 | |
Victor Stinner | 8c462c5 | 2014-01-24 18:11:43 +0100 | [diff] [blame] | 126 | Condition |
Andrew Svetlov | 28d8d14 | 2017-12-09 20:00:05 +0200 | [diff] [blame^] | 127 | --------- |
Victor Stinner | 8c462c5 | 2014-01-24 18:11:43 +0100 | [diff] [blame] | 128 | |
Andrew Svetlov | f200ce6 | 2014-07-26 19:50:37 +0300 | [diff] [blame] | 129 | .. class:: Condition(lock=None, \*, loop=None) |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 130 | |
| 131 | A Condition implementation, asynchronous equivalent to |
| 132 | :class:`threading.Condition`. |
| 133 | |
| 134 | This class implements condition variable objects. A condition variable |
| 135 | allows one or more coroutines to wait until they are notified by another |
| 136 | coroutine. |
| 137 | |
Andrew Svetlov | f200ce6 | 2014-07-26 19:50:37 +0300 | [diff] [blame] | 138 | If the *lock* argument is given and not ``None``, it must be a :class:`Lock` |
| 139 | object, and it is used as the underlying lock. Otherwise, |
| 140 | a new :class:`Lock` object is created and used as the underlying lock. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 141 | |
Andrew Svetlov | 28d8d14 | 2017-12-09 20:00:05 +0200 | [diff] [blame^] | 142 | Conditions support the :ref:`context management protocol |
| 143 | <async-with-locks>`. |
| 144 | |
Victor Stinner | 7b2c3c6 | 2015-02-26 10:39:16 +0100 | [diff] [blame] | 145 | This class is :ref:`not thread safe <asyncio-multithreading>`. |
| 146 | |
Victor Stinner | bdd574d | 2015-02-12 22:49:18 +0100 | [diff] [blame] | 147 | .. coroutinemethod:: acquire() |
Larry Hastings | 3732ed2 | 2014-03-15 21:13:56 -0700 | [diff] [blame] | 148 | |
| 149 | Acquire the underlying lock. |
| 150 | |
| 151 | This method blocks until the lock is unlocked, then sets it to locked and |
| 152 | returns ``True``. |
| 153 | |
| 154 | This method is a :ref:`coroutine <coroutine>`. |
| 155 | |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 156 | .. method:: notify(n=1) |
| 157 | |
| 158 | By default, wake up one coroutine waiting on this condition, if any. |
| 159 | If the calling coroutine has not acquired the lock when this method is |
| 160 | called, a :exc:`RuntimeError` is raised. |
| 161 | |
| 162 | This method wakes up at most *n* of the coroutines waiting for the |
| 163 | condition variable; it is a no-op if no coroutines are waiting. |
| 164 | |
| 165 | .. note:: |
| 166 | |
| 167 | An awakened coroutine does not actually return from its :meth:`wait` |
| 168 | call until it can reacquire the lock. Since :meth:`notify` does not |
| 169 | release the lock, its caller should. |
| 170 | |
Larry Hastings | 3732ed2 | 2014-03-15 21:13:56 -0700 | [diff] [blame] | 171 | .. method:: locked() |
| 172 | |
| 173 | Return ``True`` if the underlying lock is acquired. |
| 174 | |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 175 | .. method:: notify_all() |
| 176 | |
Zachary Ware | 828946e | 2015-05-06 20:19:06 -0500 | [diff] [blame] | 177 | Wake up all coroutines waiting on this condition. This method acts like |
| 178 | :meth:`notify`, but wakes up all waiting coroutines instead of one. If the |
| 179 | calling coroutine has not acquired the lock when this method is called, a |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 180 | :exc:`RuntimeError` is raised. |
| 181 | |
Larry Hastings | 3732ed2 | 2014-03-15 21:13:56 -0700 | [diff] [blame] | 182 | .. method:: release() |
| 183 | |
| 184 | Release the underlying lock. |
| 185 | |
| 186 | When the lock is locked, reset it to unlocked, and return. If any other |
| 187 | coroutines are blocked waiting for the lock to become unlocked, allow |
| 188 | exactly one of them to proceed. |
| 189 | |
| 190 | When invoked on an unlocked lock, a :exc:`RuntimeError` is raised. |
| 191 | |
| 192 | There is no return value. |
| 193 | |
Victor Stinner | bdd574d | 2015-02-12 22:49:18 +0100 | [diff] [blame] | 194 | .. coroutinemethod:: wait() |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 195 | |
| 196 | Wait until notified. |
| 197 | |
| 198 | If the calling coroutine has not acquired the lock when this method is |
| 199 | called, a :exc:`RuntimeError` is raised. |
| 200 | |
| 201 | This method releases the underlying lock, and then blocks until it is |
| 202 | awakened by a :meth:`notify` or :meth:`notify_all` call for the same |
| 203 | condition variable in another coroutine. Once awakened, it re-acquires |
| 204 | the lock and returns ``True``. |
| 205 | |
Yury Selivanov | 37f15bc | 2014-02-20 16:20:44 -0500 | [diff] [blame] | 206 | This method is a :ref:`coroutine <coroutine>`. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 207 | |
Victor Stinner | bdd574d | 2015-02-12 22:49:18 +0100 | [diff] [blame] | 208 | .. coroutinemethod:: wait_for(predicate) |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 209 | |
| 210 | Wait until a predicate becomes true. |
| 211 | |
| 212 | The predicate should be a callable which result will be interpreted as a |
| 213 | boolean value. The final predicate value is the return value. |
| 214 | |
Yury Selivanov | 37f15bc | 2014-02-20 16:20:44 -0500 | [diff] [blame] | 215 | This method is a :ref:`coroutine <coroutine>`. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 216 | |
| 217 | |
Victor Stinner | 8c462c5 | 2014-01-24 18:11:43 +0100 | [diff] [blame] | 218 | Semaphore |
Andrew Svetlov | 28d8d14 | 2017-12-09 20:00:05 +0200 | [diff] [blame^] | 219 | --------- |
Victor Stinner | 8c462c5 | 2014-01-24 18:11:43 +0100 | [diff] [blame] | 220 | |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 221 | .. class:: Semaphore(value=1, \*, loop=None) |
| 222 | |
| 223 | A Semaphore implementation. |
| 224 | |
| 225 | A semaphore manages an internal counter which is decremented by each |
| 226 | :meth:`acquire` call and incremented by each :meth:`release` call. The |
| 227 | counter can never go below zero; when :meth:`acquire` finds that it is zero, |
Zachary Ware | 828946e | 2015-05-06 20:19:06 -0500 | [diff] [blame] | 228 | it blocks, waiting until some other coroutine calls :meth:`release`. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 229 | |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 230 | The optional argument gives the initial value for the internal counter; it |
| 231 | defaults to ``1``. If the value given is less than ``0``, :exc:`ValueError` |
| 232 | is raised. |
| 233 | |
Andrew Svetlov | 28d8d14 | 2017-12-09 20:00:05 +0200 | [diff] [blame^] | 234 | Semaphores support the :ref:`context management protocol |
| 235 | <async-with-locks>`. |
| 236 | |
Victor Stinner | 7b2c3c6 | 2015-02-26 10:39:16 +0100 | [diff] [blame] | 237 | This class is :ref:`not thread safe <asyncio-multithreading>`. |
| 238 | |
Victor Stinner | bdd574d | 2015-02-12 22:49:18 +0100 | [diff] [blame] | 239 | .. coroutinemethod:: acquire() |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 240 | |
| 241 | Acquire a semaphore. |
| 242 | |
| 243 | If the internal counter is larger than zero on entry, decrement it by one |
| 244 | and return ``True`` immediately. If it is zero on entry, block, waiting |
| 245 | until some other coroutine has called :meth:`release` to make it larger |
| 246 | than ``0``, and then return ``True``. |
| 247 | |
Yury Selivanov | 37f15bc | 2014-02-20 16:20:44 -0500 | [diff] [blame] | 248 | This method is a :ref:`coroutine <coroutine>`. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 249 | |
| 250 | .. method:: locked() |
| 251 | |
| 252 | Returns ``True`` if semaphore can not be acquired immediately. |
| 253 | |
Andrew Svetlov | b79e012 | 2015-05-08 14:13:41 +0300 | [diff] [blame] | 254 | .. method:: release() |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 255 | |
| 256 | Release a semaphore, incrementing the internal counter by one. When it |
| 257 | was zero on entry and another coroutine is waiting for it to become |
| 258 | larger than zero again, wake up that coroutine. |
| 259 | |
| 260 | |
Victor Stinner | 8c462c5 | 2014-01-24 18:11:43 +0100 | [diff] [blame] | 261 | BoundedSemaphore |
Andrew Svetlov | 28d8d14 | 2017-12-09 20:00:05 +0200 | [diff] [blame^] | 262 | ---------------- |
Victor Stinner | 8c462c5 | 2014-01-24 18:11:43 +0100 | [diff] [blame] | 263 | |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 264 | .. class:: BoundedSemaphore(value=1, \*, loop=None) |
| 265 | |
Victor Stinner | 7b2c3c6 | 2015-02-26 10:39:16 +0100 | [diff] [blame] | 266 | A bounded semaphore implementation. Inherit from :class:`Semaphore`. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 267 | |
Victor Stinner | 7b2c3c6 | 2015-02-26 10:39:16 +0100 | [diff] [blame] | 268 | This raises :exc:`ValueError` in :meth:`~Semaphore.release` if it would |
| 269 | increase the value above the initial value. |
Andrew Svetlov | 28d8d14 | 2017-12-09 20:00:05 +0200 | [diff] [blame^] | 270 | |
| 271 | Bounded semapthores support the :ref:`context management |
| 272 | protocol <async-with-locks>`. |
| 273 | |
| 274 | This class is :ref:`not thread safe <asyncio-multithreading>`. |
| 275 | |
| 276 | |
| 277 | .. _async-with-locks: |
| 278 | |
| 279 | Using locks, conditions and semaphores in the :keyword:`async with` statement |
| 280 | ----------------------------------------------------------------------------- |
| 281 | |
| 282 | :class:`Lock`, :class:`Condition`, :class:`Semaphore`, and |
| 283 | :class:`BoundedSemaphore` objects can be used in :keyword:`async with` |
| 284 | statements. |
| 285 | |
| 286 | The :meth:`acquire` method will be called when the block is entered, |
| 287 | and :meth:`release` will be called when the block is exited. Hence, |
| 288 | the following snippet:: |
| 289 | |
| 290 | async with lock: |
| 291 | # do something... |
| 292 | |
| 293 | is equivalent to:: |
| 294 | |
| 295 | await lock.acquire() |
| 296 | try: |
| 297 | # do something... |
| 298 | finally: |
| 299 | lock.release() |
| 300 | |
| 301 | .. deprecated:: 3.7 |
| 302 | |
| 303 | Lock acquiring using ``await lock`` or ``yield from lock`` and |
| 304 | :keyword:`with` statement (``with await lock``, ``with (yield from |
| 305 | lock)``) are deprecated. |