2013-12-20 15:37:39 -04:00
|
|
|
.. currentmodule:: asyncio
|
2018-09-17 20:35:30 -03:00
|
|
|
|
2014-01-31 22:18:58 -04:00
|
|
|
.. _asyncio-sync:
|
2013-12-02 20:08:00 -04:00
|
|
|
|
|
|
|
==========================
|
2018-09-17 20:35:30 -03:00
|
|
|
Synchronization Primitives
|
|
|
|
==========================
|
|
|
|
|
|
|
|
asyncio synchronization primitives are designed to be similar to
|
|
|
|
those of the :mod:`threading` module with two important caveats:
|
|
|
|
|
|
|
|
* asyncio primitives are not thread-safe, therefore they should not
|
|
|
|
be used for OS thread synchronization (use :mod:`threading` for
|
|
|
|
that);
|
2013-12-02 20:08:00 -04:00
|
|
|
|
2018-09-17 20:35:30 -03:00
|
|
|
* methods of these synchronization primitives do not accept the *timeout*
|
|
|
|
argument; use the :func:`asyncio.wait_for` function to perform
|
|
|
|
operations with timeouts.
|
2017-07-25 20:03:51 -03:00
|
|
|
|
2019-04-20 20:12:23 -03:00
|
|
|
asyncio has the following basic synchronization primitives:
|
2015-01-29 19:37:04 -04:00
|
|
|
|
|
|
|
* :class:`Lock`
|
|
|
|
* :class:`Event`
|
|
|
|
* :class:`Condition`
|
2015-02-25 08:55:43 -04:00
|
|
|
* :class:`Semaphore`
|
|
|
|
* :class:`BoundedSemaphore`
|
2015-01-29 19:37:04 -04:00
|
|
|
|
2018-09-17 20:35:30 -03:00
|
|
|
|
|
|
|
---------
|
2015-01-29 19:37:04 -04:00
|
|
|
|
2013-12-02 20:08:00 -04:00
|
|
|
|
2014-01-24 13:11:43 -04:00
|
|
|
Lock
|
2018-09-17 20:35:30 -03:00
|
|
|
====
|
2014-01-24 13:11:43 -04:00
|
|
|
|
2013-12-02 20:08:00 -04:00
|
|
|
.. class:: Lock(\*, loop=None)
|
|
|
|
|
2018-09-17 20:35:30 -03:00
|
|
|
Implements a mutex lock for asyncio tasks. Not thread-safe.
|
2013-12-02 20:08:00 -04:00
|
|
|
|
2018-09-17 20:35:30 -03:00
|
|
|
An asyncio lock can be used to guarantee exclusive access to a
|
|
|
|
shared resource.
|
2013-12-02 20:08:00 -04:00
|
|
|
|
2018-09-17 20:35:30 -03:00
|
|
|
The preferred way to use a Lock is an :keyword:`async with`
|
|
|
|
statement::
|
2013-12-02 20:08:00 -04:00
|
|
|
|
2018-09-17 20:35:30 -03:00
|
|
|
lock = asyncio.Lock()
|
2013-12-02 20:08:00 -04:00
|
|
|
|
2018-09-17 20:35:30 -03:00
|
|
|
# ... later
|
|
|
|
async with lock:
|
|
|
|
# access shared state
|
2013-12-02 20:08:00 -04:00
|
|
|
|
2018-09-17 20:35:30 -03:00
|
|
|
which is equivalent to::
|
2013-12-02 20:08:00 -04:00
|
|
|
|
2018-09-17 20:35:30 -03:00
|
|
|
lock = asyncio.Lock()
|
2015-02-26 05:39:16 -04:00
|
|
|
|
2018-09-17 20:35:30 -03:00
|
|
|
# ... later
|
|
|
|
await lock.acquire()
|
|
|
|
try:
|
|
|
|
# access shared state
|
|
|
|
finally:
|
|
|
|
lock.release()
|
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
|
|
|
|
2018-09-17 20:35:30 -03:00
|
|
|
Acquire the lock.
|
2013-12-02 20:08:00 -04:00
|
|
|
|
2018-09-17 20:35:30 -03:00
|
|
|
This method waits until the lock is *unlocked*, sets it to
|
|
|
|
*locked* and returns ``True``.
|
2013-12-02 20:08:00 -04:00
|
|
|
|
2019-05-29 15:24:32 -03:00
|
|
|
When more than one coroutine is blocked in :meth:`acquire`
|
|
|
|
waiting for the lock to be unlocked, only one coroutine
|
|
|
|
eventually proceeds.
|
|
|
|
|
|
|
|
Acquiring a lock is *fair*: the coroutine that proceeds will be
|
|
|
|
the first coroutine that started waiting on the lock.
|
|
|
|
|
2013-12-02 20:08:00 -04:00
|
|
|
.. method:: release()
|
|
|
|
|
2018-09-17 20:35:30 -03:00
|
|
|
Release the lock.
|
2013-12-02 20:08:00 -04:00
|
|
|
|
2018-09-17 20:35:30 -03:00
|
|
|
When the lock is *locked*, reset it to *unlocked* and return.
|
2013-12-02 20:08:00 -04:00
|
|
|
|
2018-09-17 20:35:30 -03:00
|
|
|
If the lock is *unlocked*, a :exc:`RuntimeError` is raised.
|
2013-12-02 20:08:00 -04:00
|
|
|
|
2018-09-17 20:35:30 -03:00
|
|
|
.. method:: locked()
|
|
|
|
|
|
|
|
Return ``True`` if the lock is *locked*.
|
2013-12-02 20:08:00 -04:00
|
|
|
|
|
|
|
|
2014-01-24 13:11:43 -04:00
|
|
|
Event
|
2018-09-17 20:35:30 -03:00
|
|
|
=====
|
2014-01-24 13:11:43 -04:00
|
|
|
|
2013-12-02 20:08:00 -04:00
|
|
|
.. class:: Event(\*, loop=None)
|
|
|
|
|
2018-09-17 20:35:30 -03:00
|
|
|
An event object. Not thread-safe.
|
2013-12-02 20:08:00 -04:00
|
|
|
|
2018-09-17 20:35:30 -03:00
|
|
|
An asyncio event can be used to notify multiple asyncio tasks
|
|
|
|
that some event has happened.
|
2013-12-02 20:08:00 -04:00
|
|
|
|
2018-09-17 20:35:30 -03:00
|
|
|
An Event object manages an internal 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 set to *true*. The flag is set to *false* initially.
|
2015-02-26 05:39:16 -04:00
|
|
|
|
2018-09-17 20:35:30 -03:00
|
|
|
.. _asyncio_example_sync_event:
|
2013-12-02 20:08:00 -04:00
|
|
|
|
2018-09-17 20:35:30 -03:00
|
|
|
Example::
|
2013-12-02 20:08:00 -04:00
|
|
|
|
2018-09-17 20:35:30 -03:00
|
|
|
async def waiter(event):
|
|
|
|
print('waiting for it ...')
|
|
|
|
await event.wait()
|
|
|
|
print('... got it!')
|
2013-12-02 20:08:00 -04:00
|
|
|
|
2018-09-17 20:35:30 -03:00
|
|
|
async def main():
|
|
|
|
# Create an Event object.
|
|
|
|
event = asyncio.Event()
|
2013-12-02 20:08:00 -04:00
|
|
|
|
2018-09-17 20:35:30 -03:00
|
|
|
# Spawn a Task to wait until 'event' is set.
|
|
|
|
waiter_task = asyncio.create_task(waiter(event))
|
|
|
|
|
|
|
|
# Sleep for 1 second and set the event.
|
|
|
|
await asyncio.sleep(1)
|
|
|
|
event.set()
|
2013-12-02 20:08:00 -04:00
|
|
|
|
2018-09-17 20:35:30 -03:00
|
|
|
# Wait until the waiter task is finished.
|
|
|
|
await waiter_task
|
|
|
|
|
|
|
|
asyncio.run(main())
|
2013-12-02 20:08:00 -04:00
|
|
|
|
2015-02-12 17:49:18 -04:00
|
|
|
.. coroutinemethod:: wait()
|
2013-12-02 20:08:00 -04:00
|
|
|
|
2018-09-17 20:35:30 -03:00
|
|
|
Wait until the event is set.
|
|
|
|
|
|
|
|
If the event is set, return ``True`` immediately.
|
|
|
|
Otherwise block until another task calls :meth:`set`.
|
2013-12-02 20:08:00 -04:00
|
|
|
|
2018-09-17 20:35:30 -03:00
|
|
|
.. method:: set()
|
|
|
|
|
|
|
|
Set the event.
|
2013-12-02 20:08:00 -04:00
|
|
|
|
2018-09-17 20:35:30 -03:00
|
|
|
All tasks waiting for event to be set will be immediately
|
|
|
|
awakened.
|
|
|
|
|
|
|
|
.. method:: clear()
|
|
|
|
|
|
|
|
Clear (unset) the event.
|
|
|
|
|
|
|
|
Tasks awaiting on :meth:`wait` will now block until the
|
|
|
|
:meth:`set` method is called again.
|
|
|
|
|
|
|
|
.. method:: is_set()
|
|
|
|
|
|
|
|
Return ``True`` if the event is set.
|
2013-12-02 20:08:00 -04:00
|
|
|
|
|
|
|
|
2014-01-24 13:11:43 -04:00
|
|
|
Condition
|
2018-09-17 20:35:30 -03: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
|
|
|
|
2018-09-17 20:35:30 -03:00
|
|
|
A Condition object. Not thread-safe.
|
2013-12-02 20:08:00 -04:00
|
|
|
|
2018-09-17 20:35:30 -03:00
|
|
|
An asyncio condition primitive can be used by a task to wait for
|
|
|
|
some event to happen and then get exclusive access to a shared
|
|
|
|
resource.
|
2013-12-02 20:08:00 -04:00
|
|
|
|
2018-09-17 20:35:30 -03:00
|
|
|
In essence, a Condition object combines the functionality
|
|
|
|
of an :class:`Event` and a :class:`Lock`. It is possible to have
|
|
|
|
multiple Condition objects share one Lock, which allows coordinating
|
|
|
|
exclusive access to a shared resource between different tasks
|
|
|
|
interested in particular states of that shared resource.
|
2013-12-02 20:08:00 -04:00
|
|
|
|
2018-09-17 20:35:30 -03:00
|
|
|
The optional *lock* argument must be a :class:`Lock` object or
|
|
|
|
``None``. In the latter case a new Lock object is created
|
|
|
|
automatically.
|
2017-12-09 14:00:05 -04:00
|
|
|
|
2018-09-17 20:35:30 -03:00
|
|
|
The preferred way to use a Condition is an :keyword:`async with`
|
|
|
|
statement::
|
2015-02-26 05:39:16 -04:00
|
|
|
|
2018-09-17 20:35:30 -03:00
|
|
|
cond = asyncio.Condition()
|
2014-03-16 01:13:56 -03:00
|
|
|
|
2018-09-17 20:35:30 -03:00
|
|
|
# ... later
|
|
|
|
async with cond:
|
|
|
|
await cond.wait()
|
2014-03-16 01:13:56 -03:00
|
|
|
|
2018-09-17 20:35:30 -03:00
|
|
|
which is equivalent to::
|
2014-03-16 01:13:56 -03:00
|
|
|
|
2018-09-17 20:35:30 -03:00
|
|
|
cond = asyncio.Condition()
|
2014-03-16 01:13:56 -03:00
|
|
|
|
2018-09-17 20:35:30 -03:00
|
|
|
# ... later
|
2019-10-11 14:27:16 -03:00
|
|
|
await cond.acquire()
|
2018-09-17 20:35:30 -03:00
|
|
|
try:
|
|
|
|
await cond.wait()
|
|
|
|
finally:
|
2019-10-11 14:27:16 -03:00
|
|
|
cond.release()
|
2013-12-02 20:08:00 -04:00
|
|
|
|
2018-09-17 20:35:30 -03:00
|
|
|
.. coroutinemethod:: acquire()
|
|
|
|
|
|
|
|
Acquire the underlying lock.
|
|
|
|
|
|
|
|
This method waits until the underlying lock is *unlocked*,
|
|
|
|
sets it to *locked* and returns ``True``.
|
2013-12-02 20:08:00 -04:00
|
|
|
|
2018-09-17 20:35:30 -03:00
|
|
|
.. method:: notify(n=1)
|
2013-12-02 20:08:00 -04:00
|
|
|
|
2018-09-17 20:35:30 -03:00
|
|
|
Wake up at most *n* tasks (1 by default) waiting on this
|
|
|
|
condition. The method is no-op if no tasks are waiting.
|
2013-12-02 20:08:00 -04:00
|
|
|
|
2018-09-17 20:35:30 -03:00
|
|
|
The lock must be acquired before this method is called and
|
|
|
|
released shortly after. If called with an *unlocked* lock
|
|
|
|
a :exc:`RuntimeError` error is raised.
|
2013-12-02 20:08:00 -04:00
|
|
|
|
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()
|
|
|
|
|
2018-09-17 20:35:30 -03:00
|
|
|
Wake up all tasks waiting on this condition.
|
2013-12-02 20:08:00 -04:00
|
|
|
|
2018-09-17 20:35:30 -03:00
|
|
|
This method acts like :meth:`notify`, but wakes up all waiting
|
|
|
|
tasks.
|
2014-03-16 01:13:56 -03:00
|
|
|
|
2018-09-17 20:35:30 -03:00
|
|
|
The lock must be acquired before this method is called and
|
|
|
|
released shortly after. If called with an *unlocked* lock
|
|
|
|
a :exc:`RuntimeError` error is raised.
|
2014-03-16 01:13:56 -03:00
|
|
|
|
2018-09-17 20:35:30 -03:00
|
|
|
.. method:: release()
|
2014-03-16 01:13:56 -03:00
|
|
|
|
2018-09-17 20:35:30 -03:00
|
|
|
Release the underlying lock.
|
2014-03-16 01:13:56 -03:00
|
|
|
|
2018-09-17 20:35:30 -03:00
|
|
|
When invoked on an unlocked lock, a :exc:`RuntimeError` is
|
|
|
|
raised.
|
2014-03-16 01:13:56 -03:00
|
|
|
|
2015-02-12 17:49:18 -04:00
|
|
|
.. coroutinemethod:: wait()
|
2013-12-02 20:08:00 -04:00
|
|
|
|
|
|
|
Wait until notified.
|
|
|
|
|
2018-09-17 20:35:30 -03:00
|
|
|
If the calling task has not acquired the lock when this method is
|
2013-12-02 20:08:00 -04:00
|
|
|
called, a :exc:`RuntimeError` is raised.
|
|
|
|
|
2018-09-17 20:35:30 -03:00
|
|
|
This method releases the underlying lock, and then blocks until
|
|
|
|
it is awakened by a :meth:`notify` or :meth:`notify_all` call.
|
|
|
|
Once awakened, the Condition re-acquires its lock and this method
|
|
|
|
returns ``True``.
|
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
|
|
|
|
2018-09-17 20:35:30 -03:00
|
|
|
Wait until a predicate becomes *true*.
|
2013-12-02 20:08:00 -04:00
|
|
|
|
2018-09-17 20:35:30 -03:00
|
|
|
The predicate must be a callable which result will be
|
|
|
|
interpreted as a boolean value. The final value is the
|
|
|
|
return value.
|
2013-12-02 20:08:00 -04:00
|
|
|
|
|
|
|
|
2014-01-24 13:11:43 -04:00
|
|
|
Semaphore
|
2018-09-17 20:35:30 -03:00
|
|
|
=========
|
2014-01-24 13:11:43 -04:00
|
|
|
|
2013-12-02 20:08:00 -04:00
|
|
|
.. class:: Semaphore(value=1, \*, loop=None)
|
|
|
|
|
2018-09-17 20:35:30 -03:00
|
|
|
A Semaphore object. Not thread-safe.
|
2013-12-02 20:08:00 -04:00
|
|
|
|
|
|
|
A semaphore manages an internal counter which is decremented by each
|
2018-09-17 20:35:30 -03:00
|
|
|
: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, it blocks, waiting until some task calls
|
|
|
|
:meth:`release`.
|
|
|
|
|
|
|
|
The optional *value* argument gives the initial value for the
|
|
|
|
internal counter (``1`` by default). If the given value is
|
|
|
|
less than ``0`` a :exc:`ValueError` is raised.
|
|
|
|
|
|
|
|
The preferred way to use a Semaphore is an :keyword:`async with`
|
|
|
|
statement::
|
|
|
|
|
|
|
|
sem = asyncio.Semaphore(10)
|
2013-12-02 20:08:00 -04:00
|
|
|
|
2018-09-17 20:35:30 -03:00
|
|
|
# ... later
|
|
|
|
async with sem:
|
|
|
|
# work with shared resource
|
2013-12-02 20:08:00 -04:00
|
|
|
|
2018-09-17 20:35:30 -03:00
|
|
|
which is equivalent to::
|
2017-12-09 14:00:05 -04:00
|
|
|
|
2018-09-17 20:35:30 -03:00
|
|
|
sem = asyncio.Semaphore(10)
|
|
|
|
|
|
|
|
# ... later
|
|
|
|
await sem.acquire()
|
|
|
|
try:
|
|
|
|
# work with shared resource
|
|
|
|
finally:
|
|
|
|
sem.release()
|
2015-02-26 05:39:16 -04:00
|
|
|
|
2015-02-12 17:49:18 -04:00
|
|
|
.. coroutinemethod:: acquire()
|
2013-12-02 20:08:00 -04:00
|
|
|
|
|
|
|
Acquire a semaphore.
|
|
|
|
|
2018-09-17 20:35:30 -03:00
|
|
|
If the internal counter is greater than zero, decrement
|
|
|
|
it by one and return ``True`` immediately. If it is zero, wait
|
|
|
|
until a :meth:`release` is called and return ``True``.
|
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
|
|
|
|
2018-09-17 20:35:30 -03:00
|
|
|
Release a semaphore, incrementing the internal counter by one.
|
|
|
|
Can wake up a task waiting to acquire the semaphore.
|
|
|
|
|
|
|
|
Unlike :class:`BoundedSemaphore`, :class:`Semaphore` allows
|
|
|
|
making more ``release()`` calls than ``acquire()`` calls.
|
2013-12-02 20:08:00 -04:00
|
|
|
|
|
|
|
|
2014-01-24 13:11:43 -04:00
|
|
|
BoundedSemaphore
|
2018-09-17 20:35:30 -03:00
|
|
|
================
|
2014-01-24 13:11:43 -04:00
|
|
|
|
2013-12-02 20:08:00 -04:00
|
|
|
.. class:: BoundedSemaphore(value=1, \*, loop=None)
|
|
|
|
|
2018-09-17 20:35:30 -03:00
|
|
|
A bounded semaphore object. Not thread-safe.
|
2017-12-09 14:00:05 -04:00
|
|
|
|
2018-09-17 20:35:30 -03:00
|
|
|
Bounded Semaphore is a version of :class:`Semaphore` that raises
|
|
|
|
a :exc:`ValueError` in :meth:`~Semaphore.release` if it
|
|
|
|
increases the internal counter above the initial *value*.
|
2017-12-09 14:00:05 -04:00
|
|
|
|
|
|
|
|
2018-09-17 20:35:30 -03:00
|
|
|
---------
|
2017-12-09 14:00:05 -04:00
|
|
|
|
|
|
|
|
|
|
|
.. deprecated:: 3.7
|
|
|
|
|
2018-09-17 20:35:30 -03:00
|
|
|
Acquiring a lock using ``await lock`` or ``yield from lock`` and/or
|
2017-12-09 14:00:05 -04:00
|
|
|
:keyword:`with` statement (``with await lock``, ``with (yield from
|
2018-09-17 20:35:30 -03:00
|
|
|
lock)``) is deprecated. Use ``async with lock`` instead.
|