Skip to content

Latest commit

 

History

History
293 lines (188 loc) · 8.82 KB

asyncio-sync.rst

File metadata and controls

293 lines (188 loc) · 8.82 KB
.. currentmodule:: asyncio

Synchronization primitives

Locks:

Semaphores:

asyncio lock API was designed to be close to classes of the :mod:`threading` module (:class:`~threading.Lock`, :class:`~threading.Event`, :class:`~threading.Condition`, :class:`~threading.Semaphore`, :class:`~threading.BoundedSemaphore`), but it has no timeout parameter. The :func:`asyncio.wait_for` function can be used to cancel a task after a timeout.

Locks

Lock

Primitive lock objects.

A primitive lock is a synchronization primitive that is not owned by a particular coroutine when locked. A primitive lock is in one of two states, 'locked' or 'unlocked'.

It is created in the unlocked state. It has two basic methods, :meth:`acquire` and :meth:`release`. When the state is unlocked, acquire() changes the state to locked and returns immediately. When the state is locked, acquire() blocks until a call to release() in another coroutine changes it to unlocked, then the acquire() call resets it to locked and returns. The release() method should only be called in the locked state; it changes the state to unlocked and returns immediately. If an attempt is made to release an unlocked lock, a :exc:`RuntimeError` will be raised.

When more than one coroutine is blocked in acquire() waiting for the state to turn to unlocked, only one coroutine proceeds when a release() call resets the state to unlocked; first coroutine which is blocked in acquire() is being processed.

:meth:`acquire` is a coroutine and should be called with yield from.

Locks also support the context management protocol. (yield from lock) should be used as the context manager expression.

This class is :ref:`not thread safe <asyncio-multithreading>`.

Usage:

lock = Lock()
...
yield from lock
try:
    ...
finally:
    lock.release()

Context manager usage:

lock = Lock()
...
with (yield from lock):
    ...

Lock objects can be tested for locking state:

if not lock.locked():
    yield from lock
else:
    # lock is acquired
    ...
.. method:: locked()

   Return ``True`` if the lock is acquired.

.. coroutinemethod:: acquire()

   Acquire a lock.

   This method blocks until the lock is unlocked, then sets it to locked and
   returns ``True``.

   This method is a :ref:`coroutine <coroutine>`.

.. method:: release()

   Release a lock.

   When the lock is locked, reset it to unlocked, and return.  If any other
   coroutines are blocked waiting for the lock to become unlocked, allow
   exactly one of them to proceed.

   When invoked on an unlocked lock, a :exc:`RuntimeError` is raised.

   There is no return value.

Event

An Event implementation, asynchronous equivalent to :class:`threading.Event`.

Class implementing event objects. An event manages a flag that can be set to true with the :meth:`set` method and reset to false with the :meth:`clear` method. The :meth:`wait` method blocks until the flag is true. The flag is initially false.

This class is :ref:`not thread safe <asyncio-multithreading>`.

.. method:: clear()

   Reset the internal flag to false. Subsequently, coroutines calling
   :meth:`wait` will block until :meth:`set` is called to set the internal
   flag to true again.

.. method:: is_set()

   Return ``True`` if and only if the internal flag is true.

.. method:: set()

   Set the internal flag to true. All coroutines waiting for it to become
   true are awakened. Coroutine that call :meth:`wait` once the flag is true
   will not block at all.

.. coroutinemethod:: wait()

   Block until the internal flag is true.

   If the internal flag is true on entry, return ``True`` immediately.
   Otherwise, block until another coroutine calls :meth:`set` to set the
   flag to true, then return ``True``.

   This method is a :ref:`coroutine <coroutine>`.

Condition

Semaphores

Semaphore

BoundedSemaphore