2013-12-20 15:37:39 -04:00
|
|
|
.. currentmodule:: asyncio
|
2018-09-11 21:10:37 -03:00
|
|
|
|
2014-01-31 22:18:58 -04:00
|
|
|
.. _asyncio-sync:
|
2013-12-02 20:08:00 -04:00
|
|
|
|
|
|
|
==========================
|
2018-09-11 21:10:37 -03:00
|
|
|
Synchronization Primitives
|
|
|
|
==========================
|
|
|
|
|
2019-10-10 20:18:46 -03:00
|
|
|
**Source code:** :source:`Lib/asyncio/locks.py`
|
|
|
|
|
|
|
|
-----------------------------------------------
|
|
|
|
|
2018-09-11 21:10:37 -03:00
|
|
|
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
|
2018-09-17 20:16:44 -03:00
|
|
|
be used for OS thread synchronization (use :mod:`threading` for
|
2018-09-11 21:10:37 -03:00
|
|
|
that);
|
2013-12-02 20:08:00 -04:00
|
|
|
|
2018-09-17 20:16:44 -03:00
|
|
|
* methods of these synchronization primitives do not accept the *timeout*
|
2018-09-11 21:10:37 -03:00
|
|
|
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:06:38 -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-11 21:10:37 -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-11 21:10:37 -03:00
|
|
|
====
|
2014-01-24 13:11:43 -04:00
|
|
|
|
2013-12-02 20:08:00 -04:00
|
|
|
.. class:: Lock(\*, loop=None)
|
|
|
|
|
2018-09-11 21:10:37 -03:00
|
|
|
Implements a mutex lock for asyncio tasks. Not thread-safe.
|
2013-12-02 20:08:00 -04:00
|
|
|
|
2018-09-11 21:10:37 -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-11 21:10:37 -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-11 21:10:37 -03:00
|
|
|
lock = asyncio.Lock()
|
2013-12-02 20:08:00 -04:00
|
|
|
|
2018-09-11 21:10:37 -03:00
|
|
|
# ... later
|
|
|
|
async with lock:
|
|
|
|
# access shared state
|
2013-12-02 20:08:00 -04:00
|
|
|
|
2018-09-11 21:10:37 -03:00
|
|
|
which is equivalent to::
|
2013-12-02 20:08:00 -04:00
|
|
|
|
2018-09-11 21:10:37 -03:00
|
|
|
lock = asyncio.Lock()
|
2015-02-26 05:39:16 -04:00
|
|
|
|
2018-09-11 21:10:37 -03:00
|
|
|
# ... later
|
|
|
|
await lock.acquire()
|
|
|
|
try:
|
|
|
|
# access shared state
|
|
|
|
finally:
|
|
|
|
lock.release()
|
2013-12-02 20:08:00 -04:00
|
|
|
|
2019-09-10 07:55:07 -03:00
|
|
|
.. deprecated-removed:: 3.8 3.10
|
|
|
|
The *loop* parameter.
|
|
|
|
|
2015-02-12 17:49:18 -04:00
|
|
|
.. coroutinemethod:: acquire()
|
2013-12-02 20:08:00 -04:00
|
|
|
|
2018-09-11 21:10:37 -03:00
|
|
|
Acquire the lock.
|
2013-12-02 20:08:00 -04:00
|
|
|
|
2018-09-11 21:10:37 -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 14:08:17 -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-11 21:10:37 -03:00
|
|
|
Release the lock.
|
2013-12-02 20:08:00 -04:00
|
|
|
|
2018-09-11 21:10:37 -03:00
|
|
|
When the lock is *locked*, reset it to *unlocked* and return.
|
2013-12-02 20:08:00 -04:00
|
|
|
|
2018-09-13 22:28:19 -03:00
|
|
|
If the lock is *unlocked*, a :exc:`RuntimeError` is raised.
|
2013-12-02 20:08:00 -04:00
|
|
|
|
2018-09-11 21:10:37 -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-11 21:10:37 -03:00
|
|
|
=====
|
2014-01-24 13:11:43 -04:00
|
|
|
|
2013-12-02 20:08:00 -04:00
|
|
|
.. class:: Event(\*, loop=None)
|
|
|
|
|
2018-09-11 21:10:37 -03:00
|
|
|
An event object. Not thread-safe.
|
2013-12-02 20:08:00 -04:00
|
|
|
|
2018-09-11 21:10:37 -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-11 21:10:37 -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
|
|
|
|
2019-09-10 07:55:07 -03:00
|
|
|
|
|
|
|
.. deprecated-removed:: 3.8 3.10
|
|
|
|
The *loop* parameter.
|
|
|
|
|
2018-09-14 19:11:24 -03:00
|
|
|
.. _asyncio_example_sync_event:
|
|
|
|
|
2018-09-11 21:10:37 -03:00
|
|
|
Example::
|
2013-12-02 20:08:00 -04:00
|
|
|
|
2018-09-11 21:10:37 -03:00
|
|
|
async def waiter(event):
|
2018-09-13 22:28:19 -03:00
|
|
|
print('waiting for it ...')
|
2018-09-11 21:10:37 -03:00
|
|
|
await event.wait()
|
|
|
|
print('... got it!')
|
2013-12-02 20:08:00 -04:00
|
|
|
|
2018-09-11 21:10:37 -03:00
|
|
|
async def main():
|
|
|
|
# Create an Event object.
|
|
|
|
event = asyncio.Event()
|
2013-12-02 20:08:00 -04:00
|
|
|
|
2018-09-11 21:10:37 -03:00
|
|
|
# Spawn a Task to wait until 'event' is set.
|
|
|
|
waiter_task = asyncio.create_task(waiter(event))
|
2013-12-02 20:08:00 -04:00
|
|
|
|
2018-09-11 21:10:37 -03:00
|
|
|
# Sleep for 1 second and set the event.
|
|
|
|
await asyncio.sleep(1)
|
|
|
|
event.set()
|
|
|
|
|
|
|
|
# Wait until the waiter task is finished.
|
|
|
|
await waiter_task
|
2013-12-02 20:08:00 -04:00
|
|
|
|
2018-09-11 21:10:37 -03:00
|
|
|
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-11 21:10:37 -03:00
|
|
|
Wait until the event is set.
|
|
|
|
|
|
|
|
If the event is set, return ``True`` immediately.
|
|
|
|
Otherwise block until another task calls :meth:`set`.
|
|
|
|
|
|
|
|
.. method:: set()
|
|
|
|
|
|
|
|
Set the event.
|
2013-12-02 20:08:00 -04:00
|
|
|
|
2018-09-11 21:10:37 -03:00
|
|
|
All tasks waiting for event to be set will be immediately
|
|
|
|
awakened.
|
|
|
|
|
|
|
|
.. method:: clear()
|
2013-12-02 20:08:00 -04:00
|
|
|
|
2018-09-11 21:10:37 -03:00
|
|
|
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-11 21:10:37 -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-11 21:10:37 -03:00
|
|
|
A Condition object. Not thread-safe.
|
2013-12-02 20:08:00 -04:00
|
|
|
|
2018-09-11 21:10:37 -03:00
|
|
|
An asyncio condition primitive can be used by a task to wait for
|
2018-09-17 20:16:44 -03:00
|
|
|
some event to happen and then get exclusive access to a shared
|
2018-09-11 21:10:37 -03:00
|
|
|
resource.
|
2013-12-02 20:08:00 -04:00
|
|
|
|
2018-09-11 21:10:37 -03:00
|
|
|
In essence, a Condition object combines the functionality
|
2018-09-17 20:16:44 -03:00
|
|
|
of an :class:`Event` and a :class:`Lock`. It is possible to have
|
|
|
|
multiple Condition objects share one Lock, which allows coordinating
|
2018-09-11 21:10:37 -03:00
|
|
|
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-11 21:10:37 -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
|
|
|
|
2019-09-10 07:55:07 -03:00
|
|
|
|
|
|
|
.. deprecated-removed:: 3.8 3.10
|
|
|
|
The *loop* parameter.
|
|
|
|
|
2018-09-11 21:10:37 -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-11 21:10:37 -03:00
|
|
|
cond = asyncio.Condition()
|
2014-03-16 01:13:56 -03:00
|
|
|
|
2018-09-11 21:10:37 -03:00
|
|
|
# ... later
|
|
|
|
async with cond:
|
|
|
|
await cond.wait()
|
2014-03-16 01:13:56 -03:00
|
|
|
|
2018-09-11 21:10:37 -03:00
|
|
|
which is equivalent to::
|
2014-03-16 01:13:56 -03:00
|
|
|
|
2018-09-11 21:10:37 -03:00
|
|
|
cond = asyncio.Condition()
|
2014-03-16 01:13:56 -03:00
|
|
|
|
2018-09-11 21:10:37 -03:00
|
|
|
# ... later
|
2019-02-13 22:39:25 -04:00
|
|
|
await cond.acquire()
|
2018-09-11 21:10:37 -03:00
|
|
|
try:
|
|
|
|
await cond.wait()
|
|
|
|
finally:
|
2019-02-13 22:39:25 -04:00
|
|
|
cond.release()
|
2013-12-02 20:08:00 -04:00
|
|
|
|
2018-09-11 21:10:37 -03:00
|
|
|
.. coroutinemethod:: acquire()
|
2013-12-02 20:08:00 -04:00
|
|
|
|
2018-09-11 21:10:37 -03:00
|
|
|
Acquire the underlying lock.
|
|
|
|
|
|
|
|
This method waits until the underlying lock is *unlocked*,
|
|
|
|
sets it to *locked* and returns ``True``.
|
|
|
|
|
|
|
|
.. method:: notify(n=1)
|
2013-12-02 20:08:00 -04:00
|
|
|
|
2018-09-11 21:10:37 -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-11 21:10:37 -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-11 21:10:37 -03:00
|
|
|
Wake up all tasks waiting on this condition.
|
2013-12-02 20:08:00 -04:00
|
|
|
|
2018-09-11 21:10:37 -03:00
|
|
|
This method acts like :meth:`notify`, but wakes up all waiting
|
|
|
|
tasks.
|
2014-03-16 01:13:56 -03:00
|
|
|
|
2018-09-11 21:10:37 -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-11 21:10:37 -03:00
|
|
|
.. method:: release()
|
2014-03-16 01:13:56 -03:00
|
|
|
|
2018-09-11 21:10:37 -03:00
|
|
|
Release the underlying lock.
|
2014-03-16 01:13:56 -03:00
|
|
|
|
2018-09-11 21:10:37 -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-11 21:10:37 -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-11 21:10:37 -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-11 21:10:37 -03:00
|
|
|
Wait until a predicate becomes *true*.
|
2013-12-02 20:08:00 -04:00
|
|
|
|
2018-09-11 21:10:37 -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-11 21:10:37 -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-11 21:10:37 -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-11 21:10:37 -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.
|
|
|
|
|
2019-09-10 07:55:07 -03:00
|
|
|
|
|
|
|
.. deprecated-removed:: 3.8 3.10
|
|
|
|
The *loop* parameter.
|
|
|
|
|
2018-09-11 21:10:37 -03:00
|
|
|
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-11 21:10:37 -03:00
|
|
|
# ... later
|
|
|
|
async with sem:
|
|
|
|
# work with shared resource
|
2013-12-02 20:08:00 -04:00
|
|
|
|
2018-09-11 21:10:37 -03:00
|
|
|
which is equivalent to::
|
2017-12-09 14:00:05 -04:00
|
|
|
|
2018-09-11 21:10:37 -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-11 21:10:37 -03:00
|
|
|
If the internal counter is greater than zero, decrement
|
2018-09-17 20:16:44 -03:00
|
|
|
it by one and return ``True`` immediately. If it is zero, wait
|
2018-09-11 21:10:37 -03:00
|
|
|
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-11 21:10:37 -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
|
2018-09-17 20:16:44 -03:00
|
|
|
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-11 21:10:37 -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-11 21:10:37 -03:00
|
|
|
A bounded semaphore object. Not thread-safe.
|
2017-12-09 14:00:05 -04:00
|
|
|
|
2018-09-11 21:10:37 -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
|
|
|
|
|
|
|
|
2019-09-10 07:55:07 -03:00
|
|
|
.. deprecated-removed:: 3.8 3.10
|
|
|
|
The *loop* parameter.
|
|
|
|
|
2018-09-11 21:10:37 -03:00
|
|
|
---------
|
2017-12-09 14:00:05 -04:00
|
|
|
|
|
|
|
|
2020-02-01 07:12:52 -04:00
|
|
|
.. versionchanged:: 3.9
|
2017-12-09 14:00:05 -04:00
|
|
|
|
2018-09-11 21:10:37 -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
|
2020-02-01 07:12:52 -04:00
|
|
|
lock)``) was removed. Use ``async with lock`` instead.
|