1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
<span id="asyncio-sync"></span><h1>Synchronization Primitives</h1> <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/asyncio/locks.py">Lib/asyncio/locks.py</a></p> <p>asyncio synchronization primitives are designed to be similar to those of the <a class="reference internal" href="threading#module-threading" title="threading: Thread-based parallelism."><code>threading</code></a> module with two important caveats:</p> <ul class="simple"> <li>asyncio primitives are not thread-safe, therefore they should not be used for OS thread synchronization (use <a class="reference internal" href="threading#module-threading" title="threading: Thread-based parallelism."><code>threading</code></a> for that);</li> <li>methods of these synchronization primitives do not accept the <em>timeout</em> argument; use the <a class="reference internal" href="asyncio-task#asyncio.wait_for" title="asyncio.wait_for"><code>asyncio.wait_for()</code></a> function to perform operations with timeouts.</li> </ul> <p>asyncio has the following basic synchronization primitives:</p> <ul class="simple"> <li><a class="reference internal" href="#asyncio.Lock" title="asyncio.Lock"><code>Lock</code></a></li> <li><a class="reference internal" href="#asyncio.Event" title="asyncio.Event"><code>Event</code></a></li> <li><a class="reference internal" href="#asyncio.Condition" title="asyncio.Condition"><code>Condition</code></a></li> <li><a class="reference internal" href="#asyncio.Semaphore" title="asyncio.Semaphore"><code>Semaphore</code></a></li> <li><a class="reference internal" href="#asyncio.BoundedSemaphore" title="asyncio.BoundedSemaphore"><code>BoundedSemaphore</code></a></li> <li><a class="reference internal" href="#asyncio.Barrier" title="asyncio.Barrier"><code>Barrier</code></a></li> </ul> <section id="lock"> <h2>Lock</h2> <dl class="py class"> <dt class="sig sig-object py" id="asyncio.Lock">
<code>class asyncio.Lock</code> </dt> <dd>
<p>Implements a mutex lock for asyncio tasks. Not thread-safe.</p> <p>An asyncio lock can be used to guarantee exclusive access to a shared resource.</p> <p>The preferred way to use a Lock is an <a class="reference internal" href="../reference/compound_stmts#async-with"><code>async with</code></a> statement:</p> <pre data-language="python">lock = asyncio.Lock()
# ... later
async with lock:
# access shared state
</pre> <p>which is equivalent to:</p> <pre data-language="python">lock = asyncio.Lock()
# ... later
await lock.acquire()
try:
# access shared state
finally:
lock.release()
</pre> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.10: </span>Removed the <em>loop</em> parameter.</p> </div> <dl class="py method"> <dt class="sig sig-object py" id="asyncio.Lock.acquire">
<code>coroutine acquire()</code> </dt> <dd>
<p>Acquire the lock.</p> <p>This method waits until the lock is <em>unlocked</em>, sets it to <em>locked</em> and returns <code>True</code>.</p> <p>When more than one coroutine is blocked in <a class="reference internal" href="#asyncio.Lock.acquire" title="asyncio.Lock.acquire"><code>acquire()</code></a> waiting for the lock to be unlocked, only one coroutine eventually proceeds.</p> <p>Acquiring a lock is <em>fair</em>: the coroutine that proceeds will be the first coroutine that started waiting on the lock.</p> </dd>
</dl> <dl class="py method"> <dt class="sig sig-object py" id="asyncio.Lock.release">
<code>release()</code> </dt> <dd>
<p>Release the lock.</p> <p>When the lock is <em>locked</em>, reset it to <em>unlocked</em> and return.</p> <p>If the lock is <em>unlocked</em>, a <a class="reference internal" href="exceptions#RuntimeError" title="RuntimeError"><code>RuntimeError</code></a> is raised.</p> </dd>
</dl> <dl class="py method"> <dt class="sig sig-object py" id="asyncio.Lock.locked">
<code>locked()</code> </dt> <dd>
<p>Return <code>True</code> if the lock is <em>locked</em>.</p> </dd>
</dl> </dd>
</dl> </section> <section id="event"> <h2>Event</h2> <dl class="py class"> <dt class="sig sig-object py" id="asyncio.Event">
<code>class asyncio.Event</code> </dt> <dd>
<p>An event object. Not thread-safe.</p> <p>An asyncio event can be used to notify multiple asyncio tasks that some event has happened.</p> <p>An Event object manages an internal flag that can be set to <em>true</em> with the <a class="reference internal" href="#asyncio.Event.set" title="asyncio.Event.set"><code>set()</code></a> method and reset to <em>false</em> with the <a class="reference internal" href="#asyncio.Event.clear" title="asyncio.Event.clear"><code>clear()</code></a> method. The <a class="reference internal" href="#asyncio.Event.wait" title="asyncio.Event.wait"><code>wait()</code></a> method blocks until the flag is set to <em>true</em>. The flag is set to <em>false</em> initially.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.10: </span>Removed the <em>loop</em> parameter.</p> </div> <p id="asyncio-example-sync-event">Example:</p> <pre data-language="python">async def waiter(event):
print('waiting for it ...')
await event.wait()
print('... got it!')
async def main():
# Create an Event object.
event = asyncio.Event()
# 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()
# Wait until the waiter task is finished.
await waiter_task
asyncio.run(main())
</pre> <dl class="py method"> <dt class="sig sig-object py" id="asyncio.Event.wait">
<code>coroutine wait()</code> </dt> <dd>
<p>Wait until the event is set.</p> <p>If the event is set, return <code>True</code> immediately. Otherwise block until another task calls <a class="reference internal" href="#asyncio.Event.set" title="asyncio.Event.set"><code>set()</code></a>.</p> </dd>
</dl> <dl class="py method"> <dt class="sig sig-object py" id="asyncio.Event.set">
<code>set()</code> </dt> <dd>
<p>Set the event.</p> <p>All tasks waiting for event to be set will be immediately awakened.</p> </dd>
</dl> <dl class="py method"> <dt class="sig sig-object py" id="asyncio.Event.clear">
<code>clear()</code> </dt> <dd>
<p>Clear (unset) the event.</p> <p>Tasks awaiting on <a class="reference internal" href="#asyncio.Event.wait" title="asyncio.Event.wait"><code>wait()</code></a> will now block until the <a class="reference internal" href="#asyncio.Event.set" title="asyncio.Event.set"><code>set()</code></a> method is called again.</p> </dd>
</dl> <dl class="py method"> <dt class="sig sig-object py" id="asyncio.Event.is_set">
<code>is_set()</code> </dt> <dd>
<p>Return <code>True</code> if the event is set.</p> </dd>
</dl> </dd>
</dl> </section> <section id="condition"> <h2>Condition</h2> <dl class="py class"> <dt class="sig sig-object py" id="asyncio.Condition">
<code>class asyncio.Condition(lock=None)</code> </dt> <dd>
<p>A Condition object. Not thread-safe.</p> <p>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.</p> <p>In essence, a Condition object combines the functionality of an <a class="reference internal" href="#asyncio.Event" title="asyncio.Event"><code>Event</code></a> and a <a class="reference internal" href="#asyncio.Lock" title="asyncio.Lock"><code>Lock</code></a>. 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.</p> <p>The optional <em>lock</em> argument must be a <a class="reference internal" href="#asyncio.Lock" title="asyncio.Lock"><code>Lock</code></a> object or <code>None</code>. In the latter case a new Lock object is created automatically.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.10: </span>Removed the <em>loop</em> parameter.</p> </div> <p>The preferred way to use a Condition is an <a class="reference internal" href="../reference/compound_stmts#async-with"><code>async with</code></a> statement:</p> <pre data-language="python">cond = asyncio.Condition()
# ... later
async with cond:
await cond.wait()
</pre> <p>which is equivalent to:</p> <pre data-language="python">cond = asyncio.Condition()
# ... later
await cond.acquire()
try:
await cond.wait()
finally:
cond.release()
</pre> <dl class="py method"> <dt class="sig sig-object py" id="asyncio.Condition.acquire">
<code>coroutine acquire()</code> </dt> <dd>
<p>Acquire the underlying lock.</p> <p>This method waits until the underlying lock is <em>unlocked</em>, sets it to <em>locked</em> and returns <code>True</code>.</p> </dd>
</dl> <dl class="py method"> <dt class="sig sig-object py" id="asyncio.Condition.notify">
<code>notify(n=1)</code> </dt> <dd>
<p>Wake up at most <em>n</em> tasks (1 by default) waiting on this condition. The method is no-op if no tasks are waiting.</p> <p>The lock must be acquired before this method is called and released shortly after. If called with an <em>unlocked</em> lock a <a class="reference internal" href="exceptions#RuntimeError" title="RuntimeError"><code>RuntimeError</code></a> error is raised.</p> </dd>
</dl> <dl class="py method"> <dt class="sig sig-object py" id="asyncio.Condition.locked">
<code>locked()</code> </dt> <dd>
<p>Return <code>True</code> if the underlying lock is acquired.</p> </dd>
</dl> <dl class="py method"> <dt class="sig sig-object py" id="asyncio.Condition.notify_all">
<code>notify_all()</code> </dt> <dd>
<p>Wake up all tasks waiting on this condition.</p> <p>This method acts like <a class="reference internal" href="#asyncio.Condition.notify" title="asyncio.Condition.notify"><code>notify()</code></a>, but wakes up all waiting tasks.</p> <p>The lock must be acquired before this method is called and released shortly after. If called with an <em>unlocked</em> lock a <a class="reference internal" href="exceptions#RuntimeError" title="RuntimeError"><code>RuntimeError</code></a> error is raised.</p> </dd>
</dl> <dl class="py method"> <dt class="sig sig-object py" id="asyncio.Condition.release">
<code>release()</code> </dt> <dd>
<p>Release the underlying lock.</p> <p>When invoked on an unlocked lock, a <a class="reference internal" href="exceptions#RuntimeError" title="RuntimeError"><code>RuntimeError</code></a> is raised.</p> </dd>
</dl> <dl class="py method"> <dt class="sig sig-object py" id="asyncio.Condition.wait">
<code>coroutine wait()</code> </dt> <dd>
<p>Wait until notified.</p> <p>If the calling task has not acquired the lock when this method is called, a <a class="reference internal" href="exceptions#RuntimeError" title="RuntimeError"><code>RuntimeError</code></a> is raised.</p> <p>This method releases the underlying lock, and then blocks until it is awakened by a <a class="reference internal" href="#asyncio.Condition.notify" title="asyncio.Condition.notify"><code>notify()</code></a> or <a class="reference internal" href="#asyncio.Condition.notify_all" title="asyncio.Condition.notify_all"><code>notify_all()</code></a> call. Once awakened, the Condition re-acquires its lock and this method returns <code>True</code>.</p> </dd>
</dl> <dl class="py method"> <dt class="sig sig-object py" id="asyncio.Condition.wait_for">
<code>coroutine wait_for(predicate)</code> </dt> <dd>
<p>Wait until a predicate becomes <em>true</em>.</p> <p>The predicate must be a callable which result will be interpreted as a boolean value. The final value is the return value.</p> </dd>
</dl> </dd>
</dl> </section> <section id="semaphore"> <h2>Semaphore</h2> <dl class="py class"> <dt class="sig sig-object py" id="asyncio.Semaphore">
<code>class asyncio.Semaphore(value=1)</code> </dt> <dd>
<p>A Semaphore object. Not thread-safe.</p> <p>A semaphore manages an internal counter which is decremented by each <a class="reference internal" href="#asyncio.Semaphore.acquire" title="asyncio.Semaphore.acquire"><code>acquire()</code></a> call and incremented by each <a class="reference internal" href="#asyncio.Semaphore.release" title="asyncio.Semaphore.release"><code>release()</code></a> call. The counter can never go below zero; when <a class="reference internal" href="#asyncio.Semaphore.acquire" title="asyncio.Semaphore.acquire"><code>acquire()</code></a> finds that it is zero, it blocks, waiting until some task calls <a class="reference internal" href="#asyncio.Semaphore.release" title="asyncio.Semaphore.release"><code>release()</code></a>.</p> <p>The optional <em>value</em> argument gives the initial value for the internal counter (<code>1</code> by default). If the given value is less than <code>0</code> a <a class="reference internal" href="exceptions#ValueError" title="ValueError"><code>ValueError</code></a> is raised.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.10: </span>Removed the <em>loop</em> parameter.</p> </div> <p>The preferred way to use a Semaphore is an <a class="reference internal" href="../reference/compound_stmts#async-with"><code>async with</code></a> statement:</p> <pre data-language="python">sem = asyncio.Semaphore(10)
# ... later
async with sem:
# work with shared resource
</pre> <p>which is equivalent to:</p> <pre data-language="python">sem = asyncio.Semaphore(10)
# ... later
await sem.acquire()
try:
# work with shared resource
finally:
sem.release()
</pre> <dl class="py method"> <dt class="sig sig-object py" id="asyncio.Semaphore.acquire">
<code>coroutine acquire()</code> </dt> <dd>
<p>Acquire a semaphore.</p> <p>If the internal counter is greater than zero, decrement it by one and return <code>True</code> immediately. If it is zero, wait until a <a class="reference internal" href="#asyncio.Semaphore.release" title="asyncio.Semaphore.release"><code>release()</code></a> is called and return <code>True</code>.</p> </dd>
</dl> <dl class="py method"> <dt class="sig sig-object py" id="asyncio.Semaphore.locked">
<code>locked()</code> </dt> <dd>
<p>Returns <code>True</code> if semaphore can not be acquired immediately.</p> </dd>
</dl> <dl class="py method"> <dt class="sig sig-object py" id="asyncio.Semaphore.release">
<code>release()</code> </dt> <dd>
<p>Release a semaphore, incrementing the internal counter by one. Can wake up a task waiting to acquire the semaphore.</p> <p>Unlike <a class="reference internal" href="#asyncio.BoundedSemaphore" title="asyncio.BoundedSemaphore"><code>BoundedSemaphore</code></a>, <a class="reference internal" href="#asyncio.Semaphore" title="asyncio.Semaphore"><code>Semaphore</code></a> allows making more <code>release()</code> calls than <code>acquire()</code> calls.</p> </dd>
</dl> </dd>
</dl> </section> <section id="boundedsemaphore"> <h2>BoundedSemaphore</h2> <dl class="py class"> <dt class="sig sig-object py" id="asyncio.BoundedSemaphore">
<code>class asyncio.BoundedSemaphore(value=1)</code> </dt> <dd>
<p>A bounded semaphore object. Not thread-safe.</p> <p>Bounded Semaphore is a version of <a class="reference internal" href="#asyncio.Semaphore" title="asyncio.Semaphore"><code>Semaphore</code></a> that raises a <a class="reference internal" href="exceptions#ValueError" title="ValueError"><code>ValueError</code></a> in <a class="reference internal" href="#asyncio.Semaphore.release" title="asyncio.Semaphore.release"><code>release()</code></a> if it increases the internal counter above the initial <em>value</em>.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.10: </span>Removed the <em>loop</em> parameter.</p> </div> </dd>
</dl> </section> <section id="barrier"> <h2>Barrier</h2> <dl class="py class"> <dt class="sig sig-object py" id="asyncio.Barrier">
<code>class asyncio.Barrier(parties)</code> </dt> <dd>
<p>A barrier object. Not thread-safe.</p> <p>A barrier is a simple synchronization primitive that allows to block until <em>parties</em> number of tasks are waiting on it. Tasks can wait on the <a class="reference internal" href="#asyncio.Barrier.wait" title="asyncio.Barrier.wait"><code>wait()</code></a> method and would be blocked until the specified number of tasks end up waiting on <a class="reference internal" href="#asyncio.Barrier.wait" title="asyncio.Barrier.wait"><code>wait()</code></a>. At that point all of the waiting tasks would unblock simultaneously.</p> <p><a class="reference internal" href="../reference/compound_stmts#async-with"><code>async with</code></a> can be used as an alternative to awaiting on <a class="reference internal" href="#asyncio.Barrier.wait" title="asyncio.Barrier.wait"><code>wait()</code></a>.</p> <p>The barrier can be reused any number of times.</p> <p id="asyncio-example-barrier">Example:</p> <pre data-language="python">async def example_barrier():
# barrier with 3 parties
b = asyncio.Barrier(3)
# create 2 new waiting tasks
asyncio.create_task(b.wait())
asyncio.create_task(b.wait())
await asyncio.sleep(0)
print(b)
# The third .wait() call passes the barrier
await b.wait()
print(b)
print("barrier passed")
await asyncio.sleep(0)
print(b)
asyncio.run(example_barrier())
</pre> <p>Result of this example is:</p> <pre data-language="python"><asyncio.locks.Barrier object at 0x... [filling, waiters:2/3]>
<asyncio.locks.Barrier object at 0x... [draining, waiters:0/3]>
barrier passed
<asyncio.locks.Barrier object at 0x... [filling, waiters:0/3]>
</pre> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.11.</span></p> </div> <dl class="py method"> <dt class="sig sig-object py" id="asyncio.Barrier.wait">
<code>coroutine wait()</code> </dt> <dd>
<p>Pass the barrier. When all the tasks party to the barrier have called this function, they are all unblocked simultaneously.</p> <p>When a waiting or blocked task in the barrier is cancelled, this task exits the barrier which stays in the same state. If the state of the barrier is “filling”, the number of waiting task decreases by 1.</p> <p>The return value is an integer in the range of 0 to <code>parties-1</code>, different for each task. This can be used to select a task to do some special housekeeping, e.g.:</p> <pre data-language="python">...
async with barrier as position:
if position == 0:
# Only one task prints this
print('End of *draining phase*')
</pre> <p>This method may raise a <a class="reference internal" href="#asyncio.BrokenBarrierError" title="asyncio.BrokenBarrierError"><code>BrokenBarrierError</code></a> exception if the barrier is broken or reset while a task is waiting. It could raise a <a class="reference internal" href="asyncio-exceptions#asyncio.CancelledError" title="asyncio.CancelledError"><code>CancelledError</code></a> if a task is cancelled.</p> </dd>
</dl> <dl class="py method"> <dt class="sig sig-object py" id="asyncio.Barrier.reset">
<code>coroutine reset()</code> </dt> <dd>
<p>Return the barrier to the default, empty state. Any tasks waiting on it will receive the <a class="reference internal" href="#asyncio.BrokenBarrierError" title="asyncio.BrokenBarrierError"><code>BrokenBarrierError</code></a> exception.</p> <p>If a barrier is broken it may be better to just leave it and create a new one.</p> </dd>
</dl> <dl class="py method"> <dt class="sig sig-object py" id="asyncio.Barrier.abort">
<code>coroutine abort()</code> </dt> <dd>
<p>Put the barrier into a broken state. This causes any active or future calls to <a class="reference internal" href="asyncio-task#asyncio.wait" title="asyncio.wait"><code>wait()</code></a> to fail with the <a class="reference internal" href="#asyncio.BrokenBarrierError" title="asyncio.BrokenBarrierError"><code>BrokenBarrierError</code></a>. Use this for example if one of the tasks needs to abort, to avoid infinite waiting tasks.</p> </dd>
</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="asyncio.Barrier.parties">
<code>parties</code> </dt> <dd>
<p>The number of tasks required to pass the barrier.</p> </dd>
</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="asyncio.Barrier.n_waiting">
<code>n_waiting</code> </dt> <dd>
<p>The number of tasks currently waiting in the barrier while filling.</p> </dd>
</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="asyncio.Barrier.broken">
<code>broken</code> </dt> <dd>
<p>A boolean that is <code>True</code> if the barrier is in the broken state.</p> </dd>
</dl> </dd>
</dl> <dl class="py exception"> <dt class="sig sig-object py" id="asyncio.BrokenBarrierError">
<code>exception asyncio.BrokenBarrierError</code> </dt> <dd>
<p>This exception, a subclass of <a class="reference internal" href="exceptions#RuntimeError" title="RuntimeError"><code>RuntimeError</code></a>, is raised when the <a class="reference internal" href="#asyncio.Barrier" title="asyncio.Barrier"><code>Barrier</code></a> object is reset or broken.</p> </dd>
</dl> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.9: </span>Acquiring a lock using <code>await lock</code> or <code>yield from lock</code> and/or <a class="reference internal" href="../reference/compound_stmts#with"><code>with</code></a> statement (<code>with await lock</code>, <code>with (yield from
lock)</code>) was removed. Use <code>async with lock</code> instead.</p> </div> </section> <div class="_attribution">
<p class="_attribution-p">
© 2001–2023 Python Software Foundation<br>Licensed under the PSF License.<br>
<a href="https://docs.python.org/3.12/library/asyncio-sync.html" class="_attribution-link">https://docs.python.org/3.12/library/asyncio-sync.html</a>
</p>
</div>
|