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 | |
| 7 | Locks |
| 8 | ----- |
| 9 | |
Victor Stinner | 8c462c5 | 2014-01-24 18:11:43 +0100 | [diff] [blame] | 10 | Lock |
| 11 | ^^^^ |
| 12 | |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 13 | .. class:: Lock(\*, loop=None) |
| 14 | |
| 15 | Primitive lock objects. |
| 16 | |
| 17 | A primitive lock is a synchronization primitive that is not owned by a |
| 18 | particular coroutine when locked. A primitive lock is in one of two states, |
| 19 | 'locked' or 'unlocked'. |
| 20 | |
| 21 | It is created in the unlocked state. It has two basic methods, :meth:`acquire` |
| 22 | and :meth:`release`. When the state is unlocked, acquire() changes the state to |
| 23 | locked and returns immediately. When the state is locked, acquire() blocks |
| 24 | until a call to release() in another coroutine changes it to unlocked, then |
| 25 | the acquire() call resets it to locked and returns. The release() method |
| 26 | should only be called in the locked state; it changes the state to unlocked |
| 27 | and returns immediately. If an attempt is made to release an unlocked lock, |
| 28 | a :exc:`RuntimeError` will be raised. |
| 29 | |
| 30 | When more than one coroutine is blocked in acquire() waiting for the state |
| 31 | to turn to unlocked, only one coroutine proceeds when a release() call |
| 32 | resets the state to unlocked; first coroutine which is blocked in acquire() |
| 33 | is being processed. |
| 34 | |
| 35 | :meth:`acquire` is a coroutine and should be called with ``yield from``. |
| 36 | |
Serhiy Storchaka | 1486799 | 2014-09-10 23:43:41 +0300 | [diff] [blame] | 37 | Locks also support the context management protocol. ``(yield from lock)`` |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 38 | should be used as context manager expression. |
| 39 | |
| 40 | Usage:: |
| 41 | |
| 42 | lock = Lock() |
| 43 | ... |
| 44 | yield from lock |
| 45 | try: |
| 46 | ... |
| 47 | finally: |
| 48 | lock.release() |
| 49 | |
| 50 | Context manager usage:: |
| 51 | |
| 52 | lock = Lock() |
| 53 | ... |
| 54 | with (yield from lock): |
| 55 | ... |
| 56 | |
| 57 | Lock objects can be tested for locking state:: |
| 58 | |
| 59 | if not lock.locked(): |
| 60 | yield from lock |
| 61 | else: |
| 62 | # lock is acquired |
| 63 | ... |
| 64 | |
| 65 | .. method:: locked() |
| 66 | |
Larry Hastings | 3732ed2 | 2014-03-15 21:13:56 -0700 | [diff] [blame] | 67 | Return ``True`` if the lock is acquired. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 68 | |
| 69 | .. method:: acquire() |
| 70 | |
| 71 | Acquire a lock. |
| 72 | |
| 73 | This method blocks until the lock is unlocked, then sets it to locked and |
| 74 | returns ``True``. |
| 75 | |
Larry Hastings | 3732ed2 | 2014-03-15 21:13:56 -0700 | [diff] [blame] | 76 | This method is a :ref:`coroutine <coroutine>`. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 77 | |
| 78 | .. method:: release() |
| 79 | |
| 80 | Release a lock. |
| 81 | |
| 82 | When the lock is locked, reset it to unlocked, and return. If any other |
| 83 | coroutines are blocked waiting for the lock to become unlocked, allow |
| 84 | exactly one of them to proceed. |
| 85 | |
| 86 | When invoked on an unlocked lock, a :exc:`RuntimeError` is raised. |
| 87 | |
| 88 | There is no return value. |
| 89 | |
| 90 | |
Victor Stinner | 8c462c5 | 2014-01-24 18:11:43 +0100 | [diff] [blame] | 91 | Event |
| 92 | ^^^^^ |
| 93 | |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 94 | .. class:: Event(\*, loop=None) |
| 95 | |
| 96 | An Event implementation, asynchronous equivalent to :class:`threading.Event`. |
| 97 | |
| 98 | Class implementing event objects. An event manages a flag that can be set to |
| 99 | true with the :meth:`set` method and reset to false with the :meth:`clear` |
| 100 | method. The :meth:`wait` method blocks until the flag is true. The flag is |
| 101 | initially false. |
| 102 | |
| 103 | .. method:: clear() |
| 104 | |
| 105 | Reset the internal flag to false. Subsequently, coroutines calling |
| 106 | :meth:`wait` will block until :meth:`set` is called to set the internal |
| 107 | flag to true again. |
| 108 | |
| 109 | .. method:: is_set() |
| 110 | |
| 111 | Return ``True`` if and only if the internal flag is true. |
| 112 | |
| 113 | .. method:: set() |
| 114 | |
| 115 | Set the internal flag to true. All coroutines waiting for it to become |
| 116 | true are awakened. Coroutine that call :meth:`wait` once the flag is true |
| 117 | will not block at all. |
| 118 | |
| 119 | .. method:: wait() |
| 120 | |
| 121 | Block until the internal flag is true. |
| 122 | |
| 123 | If the internal flag is true on entry, return ``True`` immediately. |
| 124 | Otherwise, block until another coroutine calls :meth:`set` to set the |
| 125 | flag to true, then return ``True``. |
| 126 | |
Yury Selivanov | 37f15bc | 2014-02-20 16:20:44 -0500 | [diff] [blame] | 127 | This method is a :ref:`coroutine <coroutine>`. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 128 | |
| 129 | |
Victor Stinner | 8c462c5 | 2014-01-24 18:11:43 +0100 | [diff] [blame] | 130 | Condition |
| 131 | ^^^^^^^^^ |
| 132 | |
Andrew Svetlov | f200ce6 | 2014-07-26 19:50:37 +0300 | [diff] [blame] | 133 | .. class:: Condition(lock=None, \*, loop=None) |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 134 | |
| 135 | A Condition implementation, asynchronous equivalent to |
| 136 | :class:`threading.Condition`. |
| 137 | |
| 138 | This class implements condition variable objects. A condition variable |
| 139 | allows one or more coroutines to wait until they are notified by another |
| 140 | coroutine. |
| 141 | |
Andrew Svetlov | f200ce6 | 2014-07-26 19:50:37 +0300 | [diff] [blame] | 142 | If the *lock* argument is given and not ``None``, it must be a :class:`Lock` |
| 143 | object, and it is used as the underlying lock. Otherwise, |
| 144 | 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] | 145 | |
Larry Hastings | 3732ed2 | 2014-03-15 21:13:56 -0700 | [diff] [blame] | 146 | .. method:: acquire() |
| 147 | |
| 148 | Acquire the underlying lock. |
| 149 | |
| 150 | This method blocks until the lock is unlocked, then sets it to locked and |
| 151 | returns ``True``. |
| 152 | |
| 153 | This method is a :ref:`coroutine <coroutine>`. |
| 154 | |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 155 | .. method:: notify(n=1) |
| 156 | |
| 157 | By default, wake up one coroutine waiting on this condition, if any. |
| 158 | If the calling coroutine has not acquired the lock when this method is |
| 159 | called, a :exc:`RuntimeError` is raised. |
| 160 | |
| 161 | This method wakes up at most *n* of the coroutines waiting for the |
| 162 | condition variable; it is a no-op if no coroutines are waiting. |
| 163 | |
| 164 | .. note:: |
| 165 | |
| 166 | An awakened coroutine does not actually return from its :meth:`wait` |
| 167 | call until it can reacquire the lock. Since :meth:`notify` does not |
| 168 | release the lock, its caller should. |
| 169 | |
Larry Hastings | 3732ed2 | 2014-03-15 21:13:56 -0700 | [diff] [blame] | 170 | .. method:: locked() |
| 171 | |
| 172 | Return ``True`` if the underlying lock is acquired. |
| 173 | |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 174 | .. method:: notify_all() |
| 175 | |
| 176 | Wake up all threads waiting on this condition. This method acts like |
| 177 | :meth:`notify`, but wakes up all waiting threads instead of one. If the |
| 178 | calling thread has not acquired the lock when this method is called, a |
| 179 | :exc:`RuntimeError` is raised. |
| 180 | |
Larry Hastings | 3732ed2 | 2014-03-15 21:13:56 -0700 | [diff] [blame] | 181 | .. method:: release() |
| 182 | |
| 183 | Release the underlying lock. |
| 184 | |
| 185 | When the lock is locked, reset it to unlocked, and return. If any other |
| 186 | coroutines are blocked waiting for the lock to become unlocked, allow |
| 187 | exactly one of them to proceed. |
| 188 | |
| 189 | When invoked on an unlocked lock, a :exc:`RuntimeError` is raised. |
| 190 | |
| 191 | There is no return value. |
| 192 | |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 193 | .. method:: wait() |
| 194 | |
| 195 | Wait until notified. |
| 196 | |
| 197 | If the calling coroutine has not acquired the lock when this method is |
| 198 | called, a :exc:`RuntimeError` is raised. |
| 199 | |
| 200 | This method releases the underlying lock, and then blocks until it is |
| 201 | awakened by a :meth:`notify` or :meth:`notify_all` call for the same |
| 202 | condition variable in another coroutine. Once awakened, it re-acquires |
| 203 | the lock and returns ``True``. |
| 204 | |
Yury Selivanov | 37f15bc | 2014-02-20 16:20:44 -0500 | [diff] [blame] | 205 | This method is a :ref:`coroutine <coroutine>`. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 206 | |
| 207 | .. method:: wait_for(predicate) |
| 208 | |
| 209 | Wait until a predicate becomes true. |
| 210 | |
| 211 | The predicate should be a callable which result will be interpreted as a |
| 212 | boolean value. The final predicate value is the return value. |
| 213 | |
Yury Selivanov | 37f15bc | 2014-02-20 16:20:44 -0500 | [diff] [blame] | 214 | This method is a :ref:`coroutine <coroutine>`. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 215 | |
| 216 | |
| 217 | Semaphores |
| 218 | ---------- |
| 219 | |
Victor Stinner | 8c462c5 | 2014-01-24 18:11:43 +0100 | [diff] [blame] | 220 | Semaphore |
| 221 | ^^^^^^^^^ |
| 222 | |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 223 | .. class:: Semaphore(value=1, \*, loop=None) |
| 224 | |
| 225 | A Semaphore implementation. |
| 226 | |
| 227 | A semaphore manages an internal counter which is decremented by each |
| 228 | :meth:`acquire` call and incremented by each :meth:`release` call. The |
| 229 | counter can never go below zero; when :meth:`acquire` finds that it is zero, |
| 230 | it blocks, waiting until some other thread calls :meth:`release`. |
| 231 | |
Serhiy Storchaka | 1486799 | 2014-09-10 23:43:41 +0300 | [diff] [blame] | 232 | Semaphores also support the context management protocol. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 233 | |
| 234 | The optional argument gives the initial value for the internal counter; it |
| 235 | defaults to ``1``. If the value given is less than ``0``, :exc:`ValueError` |
| 236 | is raised. |
| 237 | |
| 238 | .. method:: acquire() |
| 239 | |
| 240 | Acquire a semaphore. |
| 241 | |
| 242 | If the internal counter is larger than zero on entry, decrement it by one |
| 243 | and return ``True`` immediately. If it is zero on entry, block, waiting |
| 244 | until some other coroutine has called :meth:`release` to make it larger |
| 245 | than ``0``, and then return ``True``. |
| 246 | |
Yury Selivanov | 37f15bc | 2014-02-20 16:20:44 -0500 | [diff] [blame] | 247 | This method is a :ref:`coroutine <coroutine>`. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 248 | |
| 249 | .. method:: locked() |
| 250 | |
| 251 | Returns ``True`` if semaphore can not be acquired immediately. |
| 252 | |
| 253 | .. method:: release() |
| 254 | |
| 255 | Release a semaphore, incrementing the internal counter by one. When it |
| 256 | was zero on entry and another coroutine is waiting for it to become |
| 257 | larger than zero again, wake up that coroutine. |
| 258 | |
| 259 | |
Victor Stinner | 8c462c5 | 2014-01-24 18:11:43 +0100 | [diff] [blame] | 260 | BoundedSemaphore |
| 261 | ^^^^^^^^^^^^^^^^ |
| 262 | |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 263 | .. class:: BoundedSemaphore(value=1, \*, loop=None) |
| 264 | |
| 265 | A bounded semaphore implementation. Inherit from :class:`Semaphore`. |
| 266 | |
| 267 | This raises :exc:`ValueError` in :meth:`~Semaphore.release` if it would |
| 268 | increase the value above the initial value. |
| 269 | |
| 270 | |
| 271 | Queues |
| 272 | ------ |
| 273 | |
Victor Stinner | 8c462c5 | 2014-01-24 18:11:43 +0100 | [diff] [blame] | 274 | Queue |
| 275 | ^^^^^ |
| 276 | |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 277 | .. class:: Queue(maxsize=0, \*, loop=None) |
| 278 | |
| 279 | A queue, useful for coordinating producer and consumer coroutines. |
| 280 | |
| 281 | If *maxsize* is less than or equal to zero, the queue size is infinite. If |
| 282 | it is an integer greater than ``0``, then ``yield from put()`` will block |
| 283 | when the queue reaches *maxsize*, until an item is removed by :meth:`get`. |
| 284 | |
| 285 | Unlike the standard library :mod:`queue`, you can reliably know this Queue's |
Victor Stinner | 2748bc7 | 2013-12-13 10:57:04 +0100 | [diff] [blame] | 286 | size with :meth:`qsize`, since your single-threaded asyncio application won't |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 287 | be interrupted between calling :meth:`qsize` and doing an operation on the |
| 288 | Queue. |
| 289 | |
| 290 | .. method:: empty() |
| 291 | |
| 292 | Return ``True`` if the queue is empty, ``False`` otherwise. |
| 293 | |
| 294 | .. method:: full() |
| 295 | |
| 296 | Return ``True`` if there are maxsize items in the queue. |
| 297 | |
| 298 | .. note:: |
| 299 | |
| 300 | If the Queue was initialized with ``maxsize=0`` (the default), then |
| 301 | :meth:`full()` is never ``True``. |
| 302 | |
| 303 | .. method:: get() |
| 304 | |
| 305 | Remove and return an item from the queue. |
| 306 | |
| 307 | If you yield from :meth:`get()`, wait until a item is available. |
| 308 | |
Yury Selivanov | 37f15bc | 2014-02-20 16:20:44 -0500 | [diff] [blame] | 309 | This method is a :ref:`coroutine <coroutine>`. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 310 | |
| 311 | .. method:: get_nowait() |
| 312 | |
| 313 | Remove and return an item from the queue. |
| 314 | |
| 315 | Return an item if one is immediately available, else raise |
Guido van Rossum | 9ad116b | 2014-01-25 17:38:31 -0800 | [diff] [blame] | 316 | :exc:`QueueEmpty`. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 317 | |
| 318 | .. method:: put(item) |
| 319 | |
| 320 | Put an item into the queue. |
| 321 | |
| 322 | If you yield from ``put()``, wait until a free slot is available before |
| 323 | adding item. |
| 324 | |
Yury Selivanov | 37f15bc | 2014-02-20 16:20:44 -0500 | [diff] [blame] | 325 | This method is a :ref:`coroutine <coroutine>`. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 326 | |
| 327 | .. method:: put_nowait(item) |
| 328 | |
| 329 | Put an item into the queue without blocking. |
| 330 | |
Guido van Rossum | 9ad116b | 2014-01-25 17:38:31 -0800 | [diff] [blame] | 331 | If no free slot is immediately available, raise :exc:`QueueFull`. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 332 | |
| 333 | .. method:: qsize() |
| 334 | |
| 335 | Number of items in the queue. |
| 336 | |
| 337 | .. attribute:: maxsize |
| 338 | |
| 339 | Number of items allowed in the queue. |
| 340 | |
| 341 | |
Victor Stinner | 8c462c5 | 2014-01-24 18:11:43 +0100 | [diff] [blame] | 342 | PriorityQueue |
| 343 | ^^^^^^^^^^^^^ |
| 344 | |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 345 | .. class:: PriorityQueue |
| 346 | |
| 347 | A subclass of :class:`Queue`; retrieves entries in priority order (lowest |
| 348 | first). |
| 349 | |
| 350 | Entries are typically tuples of the form: (priority number, data). |
| 351 | |
| 352 | |
Victor Stinner | 8c462c5 | 2014-01-24 18:11:43 +0100 | [diff] [blame] | 353 | LifoQueue |
| 354 | ^^^^^^^^^ |
| 355 | |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 356 | .. class:: LifoQueue |
| 357 | |
| 358 | A subclass of :class:`Queue` that retrieves most recently added entries |
| 359 | first. |
| 360 | |
| 361 | |
Victor Stinner | 8c462c5 | 2014-01-24 18:11:43 +0100 | [diff] [blame] | 362 | JoinableQueue |
| 363 | ^^^^^^^^^^^^^ |
| 364 | |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 365 | .. class:: JoinableQueue |
| 366 | |
| 367 | A subclass of :class:`Queue` with :meth:`task_done` and :meth:`join` |
| 368 | methods. |
| 369 | |
| 370 | .. method:: join() |
| 371 | |
| 372 | Block until all items in the queue have been gotten and processed. |
| 373 | |
| 374 | The count of unfinished tasks goes up whenever an item is added to the |
| 375 | queue. The count goes down whenever a consumer thread calls |
| 376 | :meth:`task_done` to indicate that the item was retrieved and all work on |
| 377 | it is complete. When the count of unfinished tasks drops to zero, |
| 378 | :meth:`join` unblocks. |
| 379 | |
Yury Selivanov | 37f15bc | 2014-02-20 16:20:44 -0500 | [diff] [blame] | 380 | This method is a :ref:`coroutine <coroutine>`. |
Victor Stinner | ea3183f | 2013-12-03 01:08:00 +0100 | [diff] [blame] | 381 | |
| 382 | .. method:: task_done() |
| 383 | |
| 384 | Indicate that a formerly enqueued task is complete. |
| 385 | |
| 386 | Used by queue consumers. For each :meth:`~Queue.get` used to fetch a task, a |
| 387 | subsequent call to :meth:`task_done` tells the queue that the processing |
| 388 | on the task is complete. |
| 389 | |
| 390 | If a :meth:`join` is currently blocking, it will resume when all items |
| 391 | have been processed (meaning that a :meth:`task_done` call was received |
| 392 | for every item that had been :meth:`~Queue.put` into the queue). |
| 393 | |
| 394 | Raises :exc:`ValueError` if called more times than there were items |
| 395 | placed in the queue. |
| 396 | |
Guido van Rossum | 9ad116b | 2014-01-25 17:38:31 -0800 | [diff] [blame] | 397 | |
| 398 | Exceptions |
| 399 | ^^^^^^^^^^ |
| 400 | |
| 401 | .. exception:: QueueEmpty |
| 402 | |
| 403 | Exception raised when non-blocking :meth:`~Queue.get` (or |
| 404 | :meth:`~Queue.get_nowait`) is called |
| 405 | on a :class:`Queue` object which is empty. |
| 406 | |
| 407 | |
| 408 | .. exception:: QueueFull |
| 409 | |
| 410 | Exception raised when non-blocking :meth:`~Queue.put` (or |
| 411 | :meth:`~Queue.put_nowait`) is called |
| 412 | on a :class:`Queue` object which is full. |