blob: a7688d5120efda36f564501836bcb52778fd3204 [file] [log] [blame]
R David Murray6a143812013-12-20 14:37:39 -05001.. currentmodule:: asyncio
Yury Selivanov8be876e2018-09-11 17:10:37 -07002
Victor Stinner606ab032014-02-01 03:18:58 +01003.. _asyncio-sync:
Victor Stinnerea3183f2013-12-03 01:08:00 +01004
Yury Selivanov8be876e2018-09-11 17:10:37 -07005==========================
6Synchronization Primitives
Victor Stinnerea3183f2013-12-03 01:08:00 +01007==========================
8
Kyle Stanleyf9000642019-10-10 19:18:46 -04009**Source code:** :source:`Lib/asyncio/locks.py`
10
11-----------------------------------------------
12
Yury Selivanov8be876e2018-09-11 17:10:37 -070013asyncio synchronization primitives are designed to be similar to
14those of the :mod:`threading` module with two important caveats:
lf627d2c82017-07-25 17:03:51 -060015
Yury Selivanov8be876e2018-09-11 17:10:37 -070016* asyncio primitives are not thread-safe, therefore they should not
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -040017 be used for OS thread synchronization (use :mod:`threading` for
Yury Selivanov8be876e2018-09-11 17:10:37 -070018 that);
19
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -040020* methods of these synchronization primitives do not accept the *timeout*
Yury Selivanov8be876e2018-09-11 17:10:37 -070021 argument; use the :func:`asyncio.wait_for` function to perform
22 operations with timeouts.
23
Fredrik Averpil3e986de2019-04-21 01:06:38 +020024asyncio has the following basic synchronization primitives:
Victor Stinner7a55b882015-01-30 00:37:04 +010025
26* :class:`Lock`
27* :class:`Event`
28* :class:`Condition`
29* :class:`Semaphore`
30* :class:`BoundedSemaphore`
31
Yury Selivanov8be876e2018-09-11 17:10:37 -070032
33---------
Victor Stinner7a55b882015-01-30 00:37:04 +010034
Victor Stinnerea3183f2013-12-03 01:08:00 +010035
Victor Stinner8c462c52014-01-24 18:11:43 +010036Lock
Yury Selivanov8be876e2018-09-11 17:10:37 -070037====
Victor Stinner8c462c52014-01-24 18:11:43 +010038
Yurii Karabas86150d32020-11-29 14:50:57 +020039.. class:: Lock()
Victor Stinnerea3183f2013-12-03 01:08:00 +010040
Yury Selivanov8be876e2018-09-11 17:10:37 -070041 Implements a mutex lock for asyncio tasks. Not thread-safe.
Victor Stinnerea3183f2013-12-03 01:08:00 +010042
Yury Selivanov8be876e2018-09-11 17:10:37 -070043 An asyncio lock can be used to guarantee exclusive access to a
44 shared resource.
Victor Stinnerea3183f2013-12-03 01:08:00 +010045
Yury Selivanov8be876e2018-09-11 17:10:37 -070046 The preferred way to use a Lock is an :keyword:`async with`
47 statement::
Victor Stinnerea3183f2013-12-03 01:08:00 +010048
Yury Selivanov8be876e2018-09-11 17:10:37 -070049 lock = asyncio.Lock()
Victor Stinnerea3183f2013-12-03 01:08:00 +010050
Yury Selivanov8be876e2018-09-11 17:10:37 -070051 # ... later
52 async with lock:
53 # access shared state
Victor Stinnerea3183f2013-12-03 01:08:00 +010054
Yury Selivanov8be876e2018-09-11 17:10:37 -070055 which is equivalent to::
Victor Stinnerea3183f2013-12-03 01:08:00 +010056
Yury Selivanov8be876e2018-09-11 17:10:37 -070057 lock = asyncio.Lock()
Victor Stinner7b2c3c62015-02-26 10:39:16 +010058
Yury Selivanov8be876e2018-09-11 17:10:37 -070059 # ... later
60 await lock.acquire()
61 try:
62 # access shared state
63 finally:
64 lock.release()
Victor Stinnerea3183f2013-12-03 01:08:00 +010065
Victor Stinnerbdd574d2015-02-12 22:49:18 +010066 .. coroutinemethod:: acquire()
Victor Stinnerea3183f2013-12-03 01:08:00 +010067
Yury Selivanov8be876e2018-09-11 17:10:37 -070068 Acquire the lock.
Victor Stinnerea3183f2013-12-03 01:08:00 +010069
Yury Selivanov8be876e2018-09-11 17:10:37 -070070 This method waits until the lock is *unlocked*, sets it to
71 *locked* and returns ``True``.
Victor Stinnerea3183f2013-12-03 01:08:00 +010072
Hrvoje Nikšić34f4f5e2019-05-29 19:08:17 +020073 When more than one coroutine is blocked in :meth:`acquire`
74 waiting for the lock to be unlocked, only one coroutine
75 eventually proceeds.
76
77 Acquiring a lock is *fair*: the coroutine that proceeds will be
78 the first coroutine that started waiting on the lock.
79
Victor Stinnerea3183f2013-12-03 01:08:00 +010080 .. method:: release()
81
Yury Selivanov8be876e2018-09-11 17:10:37 -070082 Release the lock.
Victor Stinnerea3183f2013-12-03 01:08:00 +010083
Yury Selivanov8be876e2018-09-11 17:10:37 -070084 When the lock is *locked*, reset it to *unlocked* and return.
Victor Stinnerea3183f2013-12-03 01:08:00 +010085
Carol Willing4e824e92018-09-13 18:28:19 -070086 If the lock is *unlocked*, a :exc:`RuntimeError` is raised.
Victor Stinnerea3183f2013-12-03 01:08:00 +010087
Yury Selivanov8be876e2018-09-11 17:10:37 -070088 .. method:: locked()
89
90 Return ``True`` if the lock is *locked*.
Victor Stinnerea3183f2013-12-03 01:08:00 +010091
92
Victor Stinner8c462c52014-01-24 18:11:43 +010093Event
Yury Selivanov8be876e2018-09-11 17:10:37 -070094=====
Victor Stinner8c462c52014-01-24 18:11:43 +010095
Yurii Karabas86150d32020-11-29 14:50:57 +020096.. class:: Event()
Victor Stinnerea3183f2013-12-03 01:08:00 +010097
Yury Selivanov8be876e2018-09-11 17:10:37 -070098 An event object. Not thread-safe.
Victor Stinnerea3183f2013-12-03 01:08:00 +010099
Yury Selivanov8be876e2018-09-11 17:10:37 -0700100 An asyncio event can be used to notify multiple asyncio tasks
101 that some event has happened.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100102
Yury Selivanov8be876e2018-09-11 17:10:37 -0700103 An Event object manages an internal flag that can be set to *true*
104 with the :meth:`set` method and reset to *false* with the
105 :meth:`clear` method. The :meth:`wait` method blocks until the
106 flag is set to *true*. The flag is set to *false* initially.
Victor Stinner7b2c3c62015-02-26 10:39:16 +0100107
Yury Selivanov7372c3b2018-09-14 15:11:24 -0700108 .. _asyncio_example_sync_event:
109
Yury Selivanov8be876e2018-09-11 17:10:37 -0700110 Example::
Victor Stinnerea3183f2013-12-03 01:08:00 +0100111
Yury Selivanov8be876e2018-09-11 17:10:37 -0700112 async def waiter(event):
Carol Willing4e824e92018-09-13 18:28:19 -0700113 print('waiting for it ...')
Yury Selivanov8be876e2018-09-11 17:10:37 -0700114 await event.wait()
115 print('... got it!')
Victor Stinnerea3183f2013-12-03 01:08:00 +0100116
Yury Selivanov8be876e2018-09-11 17:10:37 -0700117 async def main():
118 # Create an Event object.
119 event = asyncio.Event()
Victor Stinnerea3183f2013-12-03 01:08:00 +0100120
Yury Selivanov8be876e2018-09-11 17:10:37 -0700121 # Spawn a Task to wait until 'event' is set.
122 waiter_task = asyncio.create_task(waiter(event))
Victor Stinnerea3183f2013-12-03 01:08:00 +0100123
Yury Selivanov8be876e2018-09-11 17:10:37 -0700124 # Sleep for 1 second and set the event.
125 await asyncio.sleep(1)
126 event.set()
Victor Stinnerea3183f2013-12-03 01:08:00 +0100127
Yury Selivanov8be876e2018-09-11 17:10:37 -0700128 # Wait until the waiter task is finished.
129 await waiter_task
130
131 asyncio.run(main())
Victor Stinnerea3183f2013-12-03 01:08:00 +0100132
Victor Stinnerbdd574d2015-02-12 22:49:18 +0100133 .. coroutinemethod:: wait()
Victor Stinnerea3183f2013-12-03 01:08:00 +0100134
Yury Selivanov8be876e2018-09-11 17:10:37 -0700135 Wait until the event is set.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100136
Yury Selivanov8be876e2018-09-11 17:10:37 -0700137 If the event is set, return ``True`` immediately.
138 Otherwise block until another task calls :meth:`set`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100139
Yury Selivanov8be876e2018-09-11 17:10:37 -0700140 .. method:: set()
141
142 Set the event.
143
144 All tasks waiting for event to be set will be immediately
145 awakened.
146
147 .. method:: clear()
148
149 Clear (unset) the event.
150
151 Tasks awaiting on :meth:`wait` will now block until the
152 :meth:`set` method is called again.
153
154 .. method:: is_set()
155
156 Return ``True`` if the event is set.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100157
158
Victor Stinner8c462c52014-01-24 18:11:43 +0100159Condition
Yury Selivanov8be876e2018-09-11 17:10:37 -0700160=========
Victor Stinner8c462c52014-01-24 18:11:43 +0100161
Yurii Karabas86150d32020-11-29 14:50:57 +0200162.. class:: Condition(lock=None)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100163
Yury Selivanov8be876e2018-09-11 17:10:37 -0700164 A Condition object. Not thread-safe.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100165
Yury Selivanov8be876e2018-09-11 17:10:37 -0700166 An asyncio condition primitive can be used by a task to wait for
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400167 some event to happen and then get exclusive access to a shared
Yury Selivanov8be876e2018-09-11 17:10:37 -0700168 resource.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100169
Yury Selivanov8be876e2018-09-11 17:10:37 -0700170 In essence, a Condition object combines the functionality
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400171 of an :class:`Event` and a :class:`Lock`. It is possible to have
172 multiple Condition objects share one Lock, which allows coordinating
Yury Selivanov8be876e2018-09-11 17:10:37 -0700173 exclusive access to a shared resource between different tasks
174 interested in particular states of that shared resource.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100175
Yury Selivanov8be876e2018-09-11 17:10:37 -0700176 The optional *lock* argument must be a :class:`Lock` object or
177 ``None``. In the latter case a new Lock object is created
178 automatically.
Andrew Svetlov28d8d142017-12-09 20:00:05 +0200179
Yury Selivanov8be876e2018-09-11 17:10:37 -0700180 The preferred way to use a Condition is an :keyword:`async with`
181 statement::
182
183 cond = asyncio.Condition()
184
185 # ... later
186 async with cond:
187 await cond.wait()
188
189 which is equivalent to::
190
191 cond = asyncio.Condition()
192
193 # ... later
Kevin Mai-Husan Chiad73ac0e2019-02-14 10:39:25 +0800194 await cond.acquire()
Yury Selivanov8be876e2018-09-11 17:10:37 -0700195 try:
196 await cond.wait()
197 finally:
Kevin Mai-Husan Chiad73ac0e2019-02-14 10:39:25 +0800198 cond.release()
Victor Stinner7b2c3c62015-02-26 10:39:16 +0100199
Victor Stinnerbdd574d2015-02-12 22:49:18 +0100200 .. coroutinemethod:: acquire()
Larry Hastings3732ed22014-03-15 21:13:56 -0700201
202 Acquire the underlying lock.
203
Yury Selivanov8be876e2018-09-11 17:10:37 -0700204 This method waits until the underlying lock is *unlocked*,
205 sets it to *locked* and returns ``True``.
Larry Hastings3732ed22014-03-15 21:13:56 -0700206
Victor Stinnerea3183f2013-12-03 01:08:00 +0100207 .. method:: notify(n=1)
208
Yury Selivanov8be876e2018-09-11 17:10:37 -0700209 Wake up at most *n* tasks (1 by default) waiting on this
210 condition. The method is no-op if no tasks are waiting.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100211
Yury Selivanov8be876e2018-09-11 17:10:37 -0700212 The lock must be acquired before this method is called and
213 released shortly after. If called with an *unlocked* lock
214 a :exc:`RuntimeError` error is raised.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100215
Larry Hastings3732ed22014-03-15 21:13:56 -0700216 .. method:: locked()
217
218 Return ``True`` if the underlying lock is acquired.
219
Victor Stinnerea3183f2013-12-03 01:08:00 +0100220 .. method:: notify_all()
221
Yury Selivanov8be876e2018-09-11 17:10:37 -0700222 Wake up all tasks waiting on this condition.
223
224 This method acts like :meth:`notify`, but wakes up all waiting
225 tasks.
226
227 The lock must be acquired before this method is called and
228 released shortly after. If called with an *unlocked* lock
229 a :exc:`RuntimeError` error is raised.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100230
Larry Hastings3732ed22014-03-15 21:13:56 -0700231 .. method:: release()
232
233 Release the underlying lock.
234
Yury Selivanov8be876e2018-09-11 17:10:37 -0700235 When invoked on an unlocked lock, a :exc:`RuntimeError` is
236 raised.
Larry Hastings3732ed22014-03-15 21:13:56 -0700237
Victor Stinnerbdd574d2015-02-12 22:49:18 +0100238 .. coroutinemethod:: wait()
Victor Stinnerea3183f2013-12-03 01:08:00 +0100239
240 Wait until notified.
241
Yury Selivanov8be876e2018-09-11 17:10:37 -0700242 If the calling task has not acquired the lock when this method is
Victor Stinnerea3183f2013-12-03 01:08:00 +0100243 called, a :exc:`RuntimeError` is raised.
244
Yury Selivanov8be876e2018-09-11 17:10:37 -0700245 This method releases the underlying lock, and then blocks until
246 it is awakened by a :meth:`notify` or :meth:`notify_all` call.
247 Once awakened, the Condition re-acquires its lock and this method
248 returns ``True``.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100249
Victor Stinnerbdd574d2015-02-12 22:49:18 +0100250 .. coroutinemethod:: wait_for(predicate)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100251
Yury Selivanov8be876e2018-09-11 17:10:37 -0700252 Wait until a predicate becomes *true*.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100253
Yury Selivanov8be876e2018-09-11 17:10:37 -0700254 The predicate must be a callable which result will be
255 interpreted as a boolean value. The final value is the
256 return value.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100257
258
Victor Stinner8c462c52014-01-24 18:11:43 +0100259Semaphore
Yury Selivanov8be876e2018-09-11 17:10:37 -0700260=========
Victor Stinner8c462c52014-01-24 18:11:43 +0100261
Yurii Karabas86150d32020-11-29 14:50:57 +0200262.. class:: Semaphore(value=1)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100263
Yury Selivanov8be876e2018-09-11 17:10:37 -0700264 A Semaphore object. Not thread-safe.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100265
266 A semaphore manages an internal counter which is decremented by each
Yury Selivanov8be876e2018-09-11 17:10:37 -0700267 :meth:`acquire` call and incremented by each :meth:`release` call.
268 The counter can never go below zero; when :meth:`acquire` finds
269 that it is zero, it blocks, waiting until some task calls
270 :meth:`release`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100271
Yury Selivanov8be876e2018-09-11 17:10:37 -0700272 The optional *value* argument gives the initial value for the
273 internal counter (``1`` by default). If the given value is
274 less than ``0`` a :exc:`ValueError` is raised.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100275
Yury Selivanov8be876e2018-09-11 17:10:37 -0700276 The preferred way to use a Semaphore is an :keyword:`async with`
277 statement::
Andrew Svetlov28d8d142017-12-09 20:00:05 +0200278
Yury Selivanov8be876e2018-09-11 17:10:37 -0700279 sem = asyncio.Semaphore(10)
280
281 # ... later
282 async with sem:
283 # work with shared resource
284
285 which is equivalent to::
286
287 sem = asyncio.Semaphore(10)
288
289 # ... later
290 await sem.acquire()
291 try:
292 # work with shared resource
293 finally:
294 sem.release()
Victor Stinner7b2c3c62015-02-26 10:39:16 +0100295
Victor Stinnerbdd574d2015-02-12 22:49:18 +0100296 .. coroutinemethod:: acquire()
Victor Stinnerea3183f2013-12-03 01:08:00 +0100297
298 Acquire a semaphore.
299
Yury Selivanov8be876e2018-09-11 17:10:37 -0700300 If the internal counter is greater than zero, decrement
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400301 it by one and return ``True`` immediately. If it is zero, wait
Yury Selivanov8be876e2018-09-11 17:10:37 -0700302 until a :meth:`release` is called and return ``True``.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100303
304 .. method:: locked()
305
306 Returns ``True`` if semaphore can not be acquired immediately.
307
Andrew Svetlovb79e0122015-05-08 14:13:41 +0300308 .. method:: release()
Victor Stinnerea3183f2013-12-03 01:08:00 +0100309
Yury Selivanov8be876e2018-09-11 17:10:37 -0700310 Release a semaphore, incrementing the internal counter by one.
311 Can wake up a task waiting to acquire the semaphore.
312
313 Unlike :class:`BoundedSemaphore`, :class:`Semaphore` allows
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400314 making more ``release()`` calls than ``acquire()`` calls.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100315
316
Victor Stinner8c462c52014-01-24 18:11:43 +0100317BoundedSemaphore
Yury Selivanov8be876e2018-09-11 17:10:37 -0700318================
Victor Stinner8c462c52014-01-24 18:11:43 +0100319
Yurii Karabas86150d32020-11-29 14:50:57 +0200320.. class:: BoundedSemaphore(value=1)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100321
Yury Selivanov8be876e2018-09-11 17:10:37 -0700322 A bounded semaphore object. Not thread-safe.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100323
Yury Selivanov8be876e2018-09-11 17:10:37 -0700324 Bounded Semaphore is a version of :class:`Semaphore` that raises
325 a :exc:`ValueError` in :meth:`~Semaphore.release` if it
326 increases the internal counter above the initial *value*.
Andrew Svetlov28d8d142017-12-09 20:00:05 +0200327
Yury Selivanov8be876e2018-09-11 17:10:37 -0700328---------
Andrew Svetlov28d8d142017-12-09 20:00:05 +0200329
Andrew Svetlov28d8d142017-12-09 20:00:05 +0200330
Andrew Svetlov90d9ba62020-02-01 13:12:52 +0200331.. versionchanged:: 3.9
Andrew Svetlov28d8d142017-12-09 20:00:05 +0200332
Yury Selivanov8be876e2018-09-11 17:10:37 -0700333 Acquiring a lock using ``await lock`` or ``yield from lock`` and/or
Andrew Svetlov28d8d142017-12-09 20:00:05 +0200334 :keyword:`with` statement (``with await lock``, ``with (yield from
Andrew Svetlov90d9ba62020-02-01 13:12:52 +0200335 lock)``) was removed. Use ``async with lock`` instead.