.. currentmodule:: asyncio
Source code: :source:`Lib/asyncio/locks.py`
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.
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'.
The lock 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 await.
Locks support the :ref:`context management protocol <async-with-locks>`.
This class is :ref:`not thread safe <asyncio-multithreading>`.
.. 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.
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>`.
Using locks, conditions and semaphores in the :keyword:`async with` statement
:class:`Lock`, :class:`Condition`, :class:`Semaphore`, and :class:`BoundedSemaphore` objects can be used in :keyword:`async with` statements.
The :meth:`acquire` method will be called when the block is entered, and :meth:`release` will be called when the block is exited. Hence, the following snippet:
async with lock:
# do something...
is equivalent to:
await lock.acquire()
try:
# do something...
finally:
lock.release()
.. deprecated:: 3.7 Lock acquiring using ``await lock`` or ``yield from lock`` and :keyword:`with` statement (``with await lock``, ``with (yield from lock)``) are deprecated.