blob: f4063db2ee86e69463a77e67882a7ece8c0b1d5c [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
Miss Islington (bot)6eb34772022-02-18 01:30:36 -080066 .. versionchanged:: 3.10
67 Removed the *loop* parameter.
Miss Islington (bot)150a8e82021-05-26 15:19:42 -070068
Victor Stinnerbdd574d2015-02-12 22:49:18 +010069 .. coroutinemethod:: acquire()
Victor Stinnerea3183f2013-12-03 01:08:00 +010070
Yury Selivanov8be876e2018-09-11 17:10:37 -070071 Acquire the lock.
Victor Stinnerea3183f2013-12-03 01:08:00 +010072
Yury Selivanov8be876e2018-09-11 17:10:37 -070073 This method waits until the lock is *unlocked*, sets it to
74 *locked* and returns ``True``.
Victor Stinnerea3183f2013-12-03 01:08:00 +010075
Hrvoje Nikšić34f4f5e2019-05-29 19:08:17 +020076 When more than one coroutine is blocked in :meth:`acquire`
77 waiting for the lock to be unlocked, only one coroutine
78 eventually proceeds.
79
80 Acquiring a lock is *fair*: the coroutine that proceeds will be
81 the first coroutine that started waiting on the lock.
82
Victor Stinnerea3183f2013-12-03 01:08:00 +010083 .. method:: release()
84
Yury Selivanov8be876e2018-09-11 17:10:37 -070085 Release the lock.
Victor Stinnerea3183f2013-12-03 01:08:00 +010086
Yury Selivanov8be876e2018-09-11 17:10:37 -070087 When the lock is *locked*, reset it to *unlocked* and return.
Victor Stinnerea3183f2013-12-03 01:08:00 +010088
Carol Willing4e824e92018-09-13 18:28:19 -070089 If the lock is *unlocked*, a :exc:`RuntimeError` is raised.
Victor Stinnerea3183f2013-12-03 01:08:00 +010090
Yury Selivanov8be876e2018-09-11 17:10:37 -070091 .. method:: locked()
92
93 Return ``True`` if the lock is *locked*.
Victor Stinnerea3183f2013-12-03 01:08:00 +010094
95
Victor Stinner8c462c52014-01-24 18:11:43 +010096Event
Yury Selivanov8be876e2018-09-11 17:10:37 -070097=====
Victor Stinner8c462c52014-01-24 18:11:43 +010098
Yurii Karabas86150d32020-11-29 14:50:57 +020099.. class:: Event()
Victor Stinnerea3183f2013-12-03 01:08:00 +0100100
Yury Selivanov8be876e2018-09-11 17:10:37 -0700101 An event object. Not thread-safe.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100102
Yury Selivanov8be876e2018-09-11 17:10:37 -0700103 An asyncio event can be used to notify multiple asyncio tasks
104 that some event has happened.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100105
Yury Selivanov8be876e2018-09-11 17:10:37 -0700106 An Event object manages an internal flag that can be set to *true*
Matt Fowlerd90ff372020-12-23 05:44:52 -0500107 with the :meth:`~Event.set` method and reset to *false* with the
108 :meth:`clear` method. The :meth:`~Event.wait` method blocks until the
Yury Selivanov8be876e2018-09-11 17:10:37 -0700109 flag is set to *true*. The flag is set to *false* initially.
Victor Stinner7b2c3c62015-02-26 10:39:16 +0100110
Miss Islington (bot)6eb34772022-02-18 01:30:36 -0800111 .. versionchanged:: 3.10
112 Removed the *loop* parameter.
Miss Islington (bot)150a8e82021-05-26 15:19:42 -0700113
Yury Selivanov7372c3b2018-09-14 15:11:24 -0700114 .. _asyncio_example_sync_event:
115
Yury Selivanov8be876e2018-09-11 17:10:37 -0700116 Example::
Victor Stinnerea3183f2013-12-03 01:08:00 +0100117
Yury Selivanov8be876e2018-09-11 17:10:37 -0700118 async def waiter(event):
Carol Willing4e824e92018-09-13 18:28:19 -0700119 print('waiting for it ...')
Yury Selivanov8be876e2018-09-11 17:10:37 -0700120 await event.wait()
121 print('... got it!')
Victor Stinnerea3183f2013-12-03 01:08:00 +0100122
Yury Selivanov8be876e2018-09-11 17:10:37 -0700123 async def main():
124 # Create an Event object.
125 event = asyncio.Event()
Victor Stinnerea3183f2013-12-03 01:08:00 +0100126
Yury Selivanov8be876e2018-09-11 17:10:37 -0700127 # Spawn a Task to wait until 'event' is set.
128 waiter_task = asyncio.create_task(waiter(event))
Victor Stinnerea3183f2013-12-03 01:08:00 +0100129
Yury Selivanov8be876e2018-09-11 17:10:37 -0700130 # Sleep for 1 second and set the event.
131 await asyncio.sleep(1)
132 event.set()
Victor Stinnerea3183f2013-12-03 01:08:00 +0100133
Yury Selivanov8be876e2018-09-11 17:10:37 -0700134 # Wait until the waiter task is finished.
135 await waiter_task
136
137 asyncio.run(main())
Victor Stinnerea3183f2013-12-03 01:08:00 +0100138
Victor Stinnerbdd574d2015-02-12 22:49:18 +0100139 .. coroutinemethod:: wait()
Victor Stinnerea3183f2013-12-03 01:08:00 +0100140
Yury Selivanov8be876e2018-09-11 17:10:37 -0700141 Wait until the event is set.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100142
Yury Selivanov8be876e2018-09-11 17:10:37 -0700143 If the event is set, return ``True`` immediately.
Matt Fowlerd90ff372020-12-23 05:44:52 -0500144 Otherwise block until another task calls :meth:`~Event.set`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100145
Yury Selivanov8be876e2018-09-11 17:10:37 -0700146 .. method:: set()
147
148 Set the event.
149
150 All tasks waiting for event to be set will be immediately
151 awakened.
152
153 .. method:: clear()
154
155 Clear (unset) the event.
156
Matt Fowlerd90ff372020-12-23 05:44:52 -0500157 Tasks awaiting on :meth:`~Event.wait` will now block until the
158 :meth:`~Event.set` method is called again.
Yury Selivanov8be876e2018-09-11 17:10:37 -0700159
160 .. method:: is_set()
161
162 Return ``True`` if the event is set.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100163
164
Victor Stinner8c462c52014-01-24 18:11:43 +0100165Condition
Yury Selivanov8be876e2018-09-11 17:10:37 -0700166=========
Victor Stinner8c462c52014-01-24 18:11:43 +0100167
Yurii Karabas86150d32020-11-29 14:50:57 +0200168.. class:: Condition(lock=None)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100169
Yury Selivanov8be876e2018-09-11 17:10:37 -0700170 A Condition object. Not thread-safe.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100171
Yury Selivanov8be876e2018-09-11 17:10:37 -0700172 An asyncio condition primitive can be used by a task to wait for
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400173 some event to happen and then get exclusive access to a shared
Yury Selivanov8be876e2018-09-11 17:10:37 -0700174 resource.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100175
Yury Selivanov8be876e2018-09-11 17:10:37 -0700176 In essence, a Condition object combines the functionality
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400177 of an :class:`Event` and a :class:`Lock`. It is possible to have
178 multiple Condition objects share one Lock, which allows coordinating
Yury Selivanov8be876e2018-09-11 17:10:37 -0700179 exclusive access to a shared resource between different tasks
180 interested in particular states of that shared resource.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100181
Yury Selivanov8be876e2018-09-11 17:10:37 -0700182 The optional *lock* argument must be a :class:`Lock` object or
183 ``None``. In the latter case a new Lock object is created
184 automatically.
Andrew Svetlov28d8d142017-12-09 20:00:05 +0200185
Miss Islington (bot)6eb34772022-02-18 01:30:36 -0800186 .. versionchanged:: 3.10
187 Removed the *loop* parameter.
Miss Islington (bot)150a8e82021-05-26 15:19:42 -0700188
Yury Selivanov8be876e2018-09-11 17:10:37 -0700189 The preferred way to use a Condition is an :keyword:`async with`
190 statement::
191
192 cond = asyncio.Condition()
193
194 # ... later
195 async with cond:
196 await cond.wait()
197
198 which is equivalent to::
199
200 cond = asyncio.Condition()
201
202 # ... later
Kevin Mai-Husan Chiad73ac0e2019-02-14 10:39:25 +0800203 await cond.acquire()
Yury Selivanov8be876e2018-09-11 17:10:37 -0700204 try:
205 await cond.wait()
206 finally:
Kevin Mai-Husan Chiad73ac0e2019-02-14 10:39:25 +0800207 cond.release()
Victor Stinner7b2c3c62015-02-26 10:39:16 +0100208
Victor Stinnerbdd574d2015-02-12 22:49:18 +0100209 .. coroutinemethod:: acquire()
Larry Hastings3732ed22014-03-15 21:13:56 -0700210
211 Acquire the underlying lock.
212
Yury Selivanov8be876e2018-09-11 17:10:37 -0700213 This method waits until the underlying lock is *unlocked*,
214 sets it to *locked* and returns ``True``.
Larry Hastings3732ed22014-03-15 21:13:56 -0700215
Victor Stinnerea3183f2013-12-03 01:08:00 +0100216 .. method:: notify(n=1)
217
Yury Selivanov8be876e2018-09-11 17:10:37 -0700218 Wake up at most *n* tasks (1 by default) waiting on this
219 condition. The method is no-op if no tasks are waiting.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100220
Yury Selivanov8be876e2018-09-11 17:10:37 -0700221 The lock must be acquired before this method is called and
222 released shortly after. If called with an *unlocked* lock
223 a :exc:`RuntimeError` error is raised.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100224
Larry Hastings3732ed22014-03-15 21:13:56 -0700225 .. method:: locked()
226
227 Return ``True`` if the underlying lock is acquired.
228
Victor Stinnerea3183f2013-12-03 01:08:00 +0100229 .. method:: notify_all()
230
Yury Selivanov8be876e2018-09-11 17:10:37 -0700231 Wake up all tasks waiting on this condition.
232
233 This method acts like :meth:`notify`, but wakes up all waiting
234 tasks.
235
236 The lock must be acquired before this method is called and
237 released shortly after. If called with an *unlocked* lock
238 a :exc:`RuntimeError` error is raised.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100239
Larry Hastings3732ed22014-03-15 21:13:56 -0700240 .. method:: release()
241
242 Release the underlying lock.
243
Yury Selivanov8be876e2018-09-11 17:10:37 -0700244 When invoked on an unlocked lock, a :exc:`RuntimeError` is
245 raised.
Larry Hastings3732ed22014-03-15 21:13:56 -0700246
Victor Stinnerbdd574d2015-02-12 22:49:18 +0100247 .. coroutinemethod:: wait()
Victor Stinnerea3183f2013-12-03 01:08:00 +0100248
249 Wait until notified.
250
Yury Selivanov8be876e2018-09-11 17:10:37 -0700251 If the calling task has not acquired the lock when this method is
Victor Stinnerea3183f2013-12-03 01:08:00 +0100252 called, a :exc:`RuntimeError` is raised.
253
Yury Selivanov8be876e2018-09-11 17:10:37 -0700254 This method releases the underlying lock, and then blocks until
255 it is awakened by a :meth:`notify` or :meth:`notify_all` call.
256 Once awakened, the Condition re-acquires its lock and this method
257 returns ``True``.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100258
Victor Stinnerbdd574d2015-02-12 22:49:18 +0100259 .. coroutinemethod:: wait_for(predicate)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100260
Yury Selivanov8be876e2018-09-11 17:10:37 -0700261 Wait until a predicate becomes *true*.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100262
Yury Selivanov8be876e2018-09-11 17:10:37 -0700263 The predicate must be a callable which result will be
264 interpreted as a boolean value. The final value is the
265 return value.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100266
267
Victor Stinner8c462c52014-01-24 18:11:43 +0100268Semaphore
Yury Selivanov8be876e2018-09-11 17:10:37 -0700269=========
Victor Stinner8c462c52014-01-24 18:11:43 +0100270
Yurii Karabas86150d32020-11-29 14:50:57 +0200271.. class:: Semaphore(value=1)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100272
Yury Selivanov8be876e2018-09-11 17:10:37 -0700273 A Semaphore object. Not thread-safe.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100274
275 A semaphore manages an internal counter which is decremented by each
Yury Selivanov8be876e2018-09-11 17:10:37 -0700276 :meth:`acquire` call and incremented by each :meth:`release` call.
277 The counter can never go below zero; when :meth:`acquire` finds
278 that it is zero, it blocks, waiting until some task calls
279 :meth:`release`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100280
Yury Selivanov8be876e2018-09-11 17:10:37 -0700281 The optional *value* argument gives the initial value for the
282 internal counter (``1`` by default). If the given value is
283 less than ``0`` a :exc:`ValueError` is raised.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100284
Miss Islington (bot)6eb34772022-02-18 01:30:36 -0800285 .. versionchanged:: 3.10
286 Removed the *loop* parameter.
Miss Islington (bot)150a8e82021-05-26 15:19:42 -0700287
Yury Selivanov8be876e2018-09-11 17:10:37 -0700288 The preferred way to use a Semaphore is an :keyword:`async with`
289 statement::
Andrew Svetlov28d8d142017-12-09 20:00:05 +0200290
Yury Selivanov8be876e2018-09-11 17:10:37 -0700291 sem = asyncio.Semaphore(10)
292
293 # ... later
294 async with sem:
295 # work with shared resource
296
297 which is equivalent to::
298
299 sem = asyncio.Semaphore(10)
300
301 # ... later
302 await sem.acquire()
303 try:
304 # work with shared resource
305 finally:
306 sem.release()
Victor Stinner7b2c3c62015-02-26 10:39:16 +0100307
Victor Stinnerbdd574d2015-02-12 22:49:18 +0100308 .. coroutinemethod:: acquire()
Victor Stinnerea3183f2013-12-03 01:08:00 +0100309
310 Acquire a semaphore.
311
Yury Selivanov8be876e2018-09-11 17:10:37 -0700312 If the internal counter is greater than zero, decrement
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400313 it by one and return ``True`` immediately. If it is zero, wait
Yury Selivanov8be876e2018-09-11 17:10:37 -0700314 until a :meth:`release` is called and return ``True``.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100315
316 .. method:: locked()
317
318 Returns ``True`` if semaphore can not be acquired immediately.
319
Andrew Svetlovb79e0122015-05-08 14:13:41 +0300320 .. method:: release()
Victor Stinnerea3183f2013-12-03 01:08:00 +0100321
Yury Selivanov8be876e2018-09-11 17:10:37 -0700322 Release a semaphore, incrementing the internal counter by one.
323 Can wake up a task waiting to acquire the semaphore.
324
325 Unlike :class:`BoundedSemaphore`, :class:`Semaphore` allows
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400326 making more ``release()`` calls than ``acquire()`` calls.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100327
328
Victor Stinner8c462c52014-01-24 18:11:43 +0100329BoundedSemaphore
Yury Selivanov8be876e2018-09-11 17:10:37 -0700330================
Victor Stinner8c462c52014-01-24 18:11:43 +0100331
Yurii Karabas86150d32020-11-29 14:50:57 +0200332.. class:: BoundedSemaphore(value=1)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100333
Yury Selivanov8be876e2018-09-11 17:10:37 -0700334 A bounded semaphore object. Not thread-safe.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100335
Yury Selivanov8be876e2018-09-11 17:10:37 -0700336 Bounded Semaphore is a version of :class:`Semaphore` that raises
337 a :exc:`ValueError` in :meth:`~Semaphore.release` if it
338 increases the internal counter above the initial *value*.
Andrew Svetlov28d8d142017-12-09 20:00:05 +0200339
Miss Islington (bot)6eb34772022-02-18 01:30:36 -0800340 .. versionchanged:: 3.10
341 Removed the *loop* parameter.
Miss Islington (bot)150a8e82021-05-26 15:19:42 -0700342
Yury Selivanov8be876e2018-09-11 17:10:37 -0700343---------
Andrew Svetlov28d8d142017-12-09 20:00:05 +0200344
Andrew Svetlov28d8d142017-12-09 20:00:05 +0200345
Andrew Svetlov90d9ba62020-02-01 13:12:52 +0200346.. versionchanged:: 3.9
Andrew Svetlov28d8d142017-12-09 20:00:05 +0200347
Yury Selivanov8be876e2018-09-11 17:10:37 -0700348 Acquiring a lock using ``await lock`` or ``yield from lock`` and/or
Andrew Svetlov28d8d142017-12-09 20:00:05 +0200349 :keyword:`with` statement (``with await lock``, ``with (yield from
Andrew Svetlov90d9ba62020-02-01 13:12:52 +0200350 lock)``) was removed. Use ``async with lock`` instead.