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