2013-12-20 15:37:39 -04:00
|
|
|
.. currentmodule:: asyncio
|
2014-01-31 22:18:58 -04:00
|
|
|
.. _asyncio-sync:
|
2013-12-02 20:08:00 -04:00
|
|
|
|
|
|
|
Synchronization primitives
|
|
|
|
==========================
|
|
|
|
|
2017-07-25 20:03:51 -03:00
|
|
|
**Source code:** :source:`Lib/asyncio/locks.py`
|
|
|
|
|
2015-01-29 19:37:04 -04:00
|
|
|
Locks:
|
|
|
|
|
|
|
|
* :class:`Lock`
|
|
|
|
* :class:`Event`
|
|
|
|
* :class:`Condition`
|
|
|
|
|
2015-02-25 08:55:43 -04:00
|
|
|
Semaphores:
|
2015-01-29 19:37:04 -04:00
|
|
|
|
2015-02-25 08:55:43 -04:00
|
|
|
* :class:`Semaphore`
|
|
|
|
* :class:`BoundedSemaphore`
|
2015-01-29 19:37:04 -04:00
|
|
|
|
2015-02-25 08:55:43 -04:00
|
|
|
asyncio lock API was designed to be close to classes of the :mod:`threading`
|
|
|
|
module (:class:`~threading.Lock`, :class:`~threading.Event`,
|
2015-01-29 19:37:04 -04:00
|
|
|
:class:`~threading.Condition`, :class:`~threading.Semaphore`,
|
2015-02-25 08:55:43 -04:00
|
|
|
:class:`~threading.BoundedSemaphore`), but it has no *timeout* parameter. The
|
2015-01-29 19:37:04 -04:00
|
|
|
:func:`asyncio.wait_for` function can be used to cancel a task after a timeout.
|
|
|
|
|
2013-12-02 20:08:00 -04:00
|
|
|
|
2014-01-24 13:11:43 -04:00
|
|
|
Lock
|
2017-12-09 14:00:05 -04:00
|
|
|
----
|
2014-01-24 13:11:43 -04:00
|
|
|
|
2013-12-02 20:08:00 -04:00
|
|
|
.. class:: Lock(\*, loop=None)
|
|
|
|
|
|
|
|
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'.
|
|
|
|
|
2017-12-09 14:00:05 -04:00
|
|
|
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
|
2013-12-02 20:08:00 -04:00
|
|
|
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.
|
|
|
|
|
2017-12-09 14:00:05 -04:00
|
|
|
:meth:`acquire` is a coroutine and should be called with ``await``.
|
2013-12-02 20:08:00 -04:00
|
|
|
|
2017-12-09 14:00:05 -04:00
|
|
|
Locks support the :ref:`context management protocol <async-with-locks>`.
|
2013-12-02 20:08:00 -04:00
|
|
|
|
2015-02-26 05:39:16 -04:00
|
|
|
This class is :ref:`not thread safe <asyncio-multithreading>`.
|
|
|
|
|
2013-12-02 20:08:00 -04:00
|
|
|
.. method:: locked()
|
|
|
|
|
2014-03-16 01:13:56 -03:00
|
|
|
Return ``True`` if the lock is acquired.
|
2013-12-02 20:08:00 -04:00
|
|
|
|
2015-02-12 17:49:18 -04:00
|
|
|
.. coroutinemethod:: acquire()
|
2013-12-02 20:08:00 -04:00
|
|
|
|
|
|
|
Acquire a lock.
|
|
|
|
|
|
|
|
This method blocks until the lock is unlocked, then sets it to locked and
|
|
|
|
returns ``True``.
|
|
|
|
|
2014-03-16 01:13:56 -03:00
|
|
|
This method is a :ref:`coroutine <coroutine>`.
|
2013-12-02 20:08:00 -04:00
|
|
|
|
|
|
|
.. 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.
|
|
|
|
|
|
|
|
|
2014-01-24 13:11:43 -04:00
|
|
|
Event
|
2017-12-09 14:00:05 -04:00
|
|
|
-----
|
2014-01-24 13:11:43 -04:00
|
|
|
|
2013-12-02 20:08:00 -04:00
|
|
|
.. class:: Event(\*, loop=None)
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
2015-02-26 05:39:16 -04:00
|
|
|
This class is :ref:`not thread safe <asyncio-multithreading>`.
|
|
|
|
|
2013-12-02 20:08:00 -04:00
|
|
|
.. 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.
|
|
|
|
|
2015-02-12 17:49:18 -04:00
|
|
|
.. coroutinemethod:: wait()
|
2013-12-02 20:08:00 -04:00
|
|
|
|
|
|
|
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``.
|
|
|
|
|
2014-02-20 17:20:44 -04:00
|
|
|
This method is a :ref:`coroutine <coroutine>`.
|
2013-12-02 20:08:00 -04:00
|
|
|
|
|
|
|
|
2014-01-24 13:11:43 -04:00
|
|
|
Condition
|
2017-12-09 14:00:05 -04:00
|
|
|
---------
|
2014-01-24 13:11:43 -04:00
|
|
|
|
2014-07-26 13:50:37 -03:00
|
|
|
.. class:: Condition(lock=None, \*, loop=None)
|
2013-12-02 20:08:00 -04:00
|
|
|
|
|
|
|
A Condition implementation, asynchronous equivalent to
|
|
|
|
:class:`threading.Condition`.
|
|
|
|
|
|
|
|
This class implements condition variable objects. A condition variable
|
|
|
|
allows one or more coroutines to wait until they are notified by another
|
|
|
|
coroutine.
|
|
|
|
|
2014-07-26 13:50:37 -03:00
|
|
|
If the *lock* argument is given and not ``None``, it must be a :class:`Lock`
|
|
|
|
object, and it is used as the underlying lock. Otherwise,
|
|
|
|
a new :class:`Lock` object is created and used as the underlying lock.
|
2013-12-02 20:08:00 -04:00
|
|
|
|
2017-12-09 14:00:05 -04:00
|
|
|
Conditions support the :ref:`context management protocol
|
|
|
|
<async-with-locks>`.
|
|
|
|
|
2015-02-26 05:39:16 -04:00
|
|
|
This class is :ref:`not thread safe <asyncio-multithreading>`.
|
|
|
|
|
2015-02-12 17:49:18 -04:00
|
|
|
.. coroutinemethod:: acquire()
|
2014-03-16 01:13:56 -03:00
|
|
|
|
|
|
|
Acquire the underlying lock.
|
|
|
|
|
|
|
|
This method blocks until the lock is unlocked, then sets it to locked and
|
|
|
|
returns ``True``.
|
|
|
|
|
|
|
|
This method is a :ref:`coroutine <coroutine>`.
|
|
|
|
|
2013-12-02 20:08:00 -04:00
|
|
|
.. method:: notify(n=1)
|
|
|
|
|
|
|
|
By default, wake up one coroutine waiting on this condition, if any.
|
|
|
|
If the calling coroutine has not acquired the lock when this method is
|
|
|
|
called, a :exc:`RuntimeError` is raised.
|
|
|
|
|
|
|
|
This method wakes up at most *n* of the coroutines waiting for the
|
|
|
|
condition variable; it is a no-op if no coroutines are waiting.
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
|
|
|
An awakened coroutine does not actually return from its :meth:`wait`
|
|
|
|
call until it can reacquire the lock. Since :meth:`notify` does not
|
|
|
|
release the lock, its caller should.
|
|
|
|
|
2014-03-16 01:13:56 -03:00
|
|
|
.. method:: locked()
|
|
|
|
|
|
|
|
Return ``True`` if the underlying lock is acquired.
|
|
|
|
|
2013-12-02 20:08:00 -04:00
|
|
|
.. method:: notify_all()
|
|
|
|
|
2015-05-06 22:19:06 -03:00
|
|
|
Wake up all coroutines waiting on this condition. This method acts like
|
|
|
|
:meth:`notify`, but wakes up all waiting coroutines instead of one. If the
|
|
|
|
calling coroutine has not acquired the lock when this method is called, a
|
2013-12-02 20:08:00 -04:00
|
|
|
:exc:`RuntimeError` is raised.
|
|
|
|
|
2014-03-16 01:13:56 -03:00
|
|
|
.. method:: release()
|
|
|
|
|
|
|
|
Release the underlying 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.
|
|
|
|
|
2015-02-12 17:49:18 -04:00
|
|
|
.. coroutinemethod:: wait()
|
2013-12-02 20:08:00 -04:00
|
|
|
|
|
|
|
Wait until notified.
|
|
|
|
|
|
|
|
If the calling coroutine has not acquired the lock when this method is
|
|
|
|
called, a :exc:`RuntimeError` is raised.
|
|
|
|
|
|
|
|
This method releases the underlying lock, and then blocks until it is
|
|
|
|
awakened by a :meth:`notify` or :meth:`notify_all` call for the same
|
|
|
|
condition variable in another coroutine. Once awakened, it re-acquires
|
|
|
|
the lock and returns ``True``.
|
|
|
|
|
2014-02-20 17:20:44 -04:00
|
|
|
This method is a :ref:`coroutine <coroutine>`.
|
2013-12-02 20:08:00 -04:00
|
|
|
|
2015-02-12 17:49:18 -04:00
|
|
|
.. coroutinemethod:: wait_for(predicate)
|
2013-12-02 20:08:00 -04:00
|
|
|
|
|
|
|
Wait until a predicate becomes true.
|
|
|
|
|
|
|
|
The predicate should be a callable which result will be interpreted as a
|
|
|
|
boolean value. The final predicate value is the return value.
|
|
|
|
|
2014-02-20 17:20:44 -04:00
|
|
|
This method is a :ref:`coroutine <coroutine>`.
|
2013-12-02 20:08:00 -04:00
|
|
|
|
|
|
|
|
2014-01-24 13:11:43 -04:00
|
|
|
Semaphore
|
2017-12-09 14:00:05 -04:00
|
|
|
---------
|
2014-01-24 13:11:43 -04:00
|
|
|
|
2013-12-02 20:08:00 -04:00
|
|
|
.. class:: Semaphore(value=1, \*, loop=None)
|
|
|
|
|
|
|
|
A Semaphore implementation.
|
|
|
|
|
|
|
|
A semaphore manages an internal counter which is decremented by each
|
|
|
|
:meth:`acquire` call and incremented by each :meth:`release` call. The
|
|
|
|
counter can never go below zero; when :meth:`acquire` finds that it is zero,
|
2015-05-06 22:19:06 -03:00
|
|
|
it blocks, waiting until some other coroutine calls :meth:`release`.
|
2013-12-02 20:08:00 -04:00
|
|
|
|
|
|
|
The optional argument gives the initial value for the internal counter; it
|
|
|
|
defaults to ``1``. If the value given is less than ``0``, :exc:`ValueError`
|
|
|
|
is raised.
|
|
|
|
|
2017-12-09 14:00:05 -04:00
|
|
|
Semaphores support the :ref:`context management protocol
|
|
|
|
<async-with-locks>`.
|
|
|
|
|
2015-02-26 05:39:16 -04:00
|
|
|
This class is :ref:`not thread safe <asyncio-multithreading>`.
|
|
|
|
|
2015-02-12 17:49:18 -04:00
|
|
|
.. coroutinemethod:: acquire()
|
2013-12-02 20:08:00 -04:00
|
|
|
|
|
|
|
Acquire a semaphore.
|
|
|
|
|
|
|
|
If the internal counter is larger than zero on entry, decrement it by one
|
|
|
|
and return ``True`` immediately. If it is zero on entry, block, waiting
|
|
|
|
until some other coroutine has called :meth:`release` to make it larger
|
|
|
|
than ``0``, and then return ``True``.
|
|
|
|
|
2014-02-20 17:20:44 -04:00
|
|
|
This method is a :ref:`coroutine <coroutine>`.
|
2013-12-02 20:08:00 -04:00
|
|
|
|
|
|
|
.. method:: locked()
|
|
|
|
|
|
|
|
Returns ``True`` if semaphore can not be acquired immediately.
|
|
|
|
|
2015-05-08 08:13:41 -03:00
|
|
|
.. method:: release()
|
2013-12-02 20:08:00 -04:00
|
|
|
|
|
|
|
Release a semaphore, incrementing the internal counter by one. When it
|
|
|
|
was zero on entry and another coroutine is waiting for it to become
|
|
|
|
larger than zero again, wake up that coroutine.
|
|
|
|
|
|
|
|
|
2014-01-24 13:11:43 -04:00
|
|
|
BoundedSemaphore
|
2017-12-09 14:00:05 -04:00
|
|
|
----------------
|
2014-01-24 13:11:43 -04:00
|
|
|
|
2013-12-02 20:08:00 -04:00
|
|
|
.. class:: BoundedSemaphore(value=1, \*, loop=None)
|
|
|
|
|
2015-02-26 05:39:16 -04:00
|
|
|
A bounded semaphore implementation. Inherit from :class:`Semaphore`.
|
2013-12-02 20:08:00 -04:00
|
|
|
|
2015-02-26 05:39:16 -04:00
|
|
|
This raises :exc:`ValueError` in :meth:`~Semaphore.release` if it would
|
|
|
|
increase the value above the initial value.
|
2017-12-09 14:00:05 -04:00
|
|
|
|
|
|
|
Bounded semapthores support the :ref:`context management
|
|
|
|
protocol <async-with-locks>`.
|
|
|
|
|
|
|
|
This class is :ref:`not thread safe <asyncio-multithreading>`.
|
|
|
|
|
|
|
|
|
|
|
|
.. _async-with-locks:
|
|
|
|
|
|
|
|
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.
|