blob: 88e523af0b80882ce7da534d63a8d338f42c52fb [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)150a8e82021-05-26 15:19:42 -070066 .. deprecated-removed:: 3.8 3.10
67 The ``loop`` parameter. This class has been implicitly getting the
68 current running loop since 3.7. See
69 :ref:`What's New in 3.10's Removed section <whatsnew310-removed>`
70 for more information.
71
Victor Stinnerbdd574d2015-02-12 22:49:18 +010072 .. coroutinemethod:: acquire()
Victor Stinnerea3183f2013-12-03 01:08:00 +010073
Yury Selivanov8be876e2018-09-11 17:10:37 -070074 Acquire the lock.
Victor Stinnerea3183f2013-12-03 01:08:00 +010075
Yury Selivanov8be876e2018-09-11 17:10:37 -070076 This method waits until the lock is *unlocked*, sets it to
77 *locked* and returns ``True``.
Victor Stinnerea3183f2013-12-03 01:08:00 +010078
Hrvoje Nikšić34f4f5e2019-05-29 19:08:17 +020079 When more than one coroutine is blocked in :meth:`acquire`
80 waiting for the lock to be unlocked, only one coroutine
81 eventually proceeds.
82
83 Acquiring a lock is *fair*: the coroutine that proceeds will be
84 the first coroutine that started waiting on the lock.
85
Victor Stinnerea3183f2013-12-03 01:08:00 +010086 .. method:: release()
87
Yury Selivanov8be876e2018-09-11 17:10:37 -070088 Release the lock.
Victor Stinnerea3183f2013-12-03 01:08:00 +010089
Yury Selivanov8be876e2018-09-11 17:10:37 -070090 When the lock is *locked*, reset it to *unlocked* and return.
Victor Stinnerea3183f2013-12-03 01:08:00 +010091
Carol Willing4e824e92018-09-13 18:28:19 -070092 If the lock is *unlocked*, a :exc:`RuntimeError` is raised.
Victor Stinnerea3183f2013-12-03 01:08:00 +010093
Yury Selivanov8be876e2018-09-11 17:10:37 -070094 .. method:: locked()
95
96 Return ``True`` if the lock is *locked*.
Victor Stinnerea3183f2013-12-03 01:08:00 +010097
98
Victor Stinner8c462c52014-01-24 18:11:43 +010099Event
Yury Selivanov8be876e2018-09-11 17:10:37 -0700100=====
Victor Stinner8c462c52014-01-24 18:11:43 +0100101
Yurii Karabas86150d32020-11-29 14:50:57 +0200102.. class:: Event()
Victor Stinnerea3183f2013-12-03 01:08:00 +0100103
Yury Selivanov8be876e2018-09-11 17:10:37 -0700104 An event object. Not thread-safe.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100105
Yury Selivanov8be876e2018-09-11 17:10:37 -0700106 An asyncio event can be used to notify multiple asyncio tasks
107 that some event has happened.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100108
Yury Selivanov8be876e2018-09-11 17:10:37 -0700109 An Event object manages an internal flag that can be set to *true*
Matt Fowlerd90ff372020-12-23 05:44:52 -0500110 with the :meth:`~Event.set` method and reset to *false* with the
111 :meth:`clear` method. The :meth:`~Event.wait` method blocks until the
Yury Selivanov8be876e2018-09-11 17:10:37 -0700112 flag is set to *true*. The flag is set to *false* initially.
Victor Stinner7b2c3c62015-02-26 10:39:16 +0100113
Miss Islington (bot)150a8e82021-05-26 15:19:42 -0700114 .. deprecated-removed:: 3.8 3.10
115 The ``loop`` parameter. This class has been implicitly getting the
116 current running loop since 3.7. See
117 :ref:`What's New in 3.10's Removed section <whatsnew310-removed>`
118 for more information.
119
Yury Selivanov7372c3b2018-09-14 15:11:24 -0700120 .. _asyncio_example_sync_event:
121
Yury Selivanov8be876e2018-09-11 17:10:37 -0700122 Example::
Victor Stinnerea3183f2013-12-03 01:08:00 +0100123
Yury Selivanov8be876e2018-09-11 17:10:37 -0700124 async def waiter(event):
Carol Willing4e824e92018-09-13 18:28:19 -0700125 print('waiting for it ...')
Yury Selivanov8be876e2018-09-11 17:10:37 -0700126 await event.wait()
127 print('... got it!')
Victor Stinnerea3183f2013-12-03 01:08:00 +0100128
Yury Selivanov8be876e2018-09-11 17:10:37 -0700129 async def main():
130 # Create an Event object.
131 event = asyncio.Event()
Victor Stinnerea3183f2013-12-03 01:08:00 +0100132
Yury Selivanov8be876e2018-09-11 17:10:37 -0700133 # Spawn a Task to wait until 'event' is set.
134 waiter_task = asyncio.create_task(waiter(event))
Victor Stinnerea3183f2013-12-03 01:08:00 +0100135
Yury Selivanov8be876e2018-09-11 17:10:37 -0700136 # Sleep for 1 second and set the event.
137 await asyncio.sleep(1)
138 event.set()
Victor Stinnerea3183f2013-12-03 01:08:00 +0100139
Yury Selivanov8be876e2018-09-11 17:10:37 -0700140 # Wait until the waiter task is finished.
141 await waiter_task
142
143 asyncio.run(main())
Victor Stinnerea3183f2013-12-03 01:08:00 +0100144
Victor Stinnerbdd574d2015-02-12 22:49:18 +0100145 .. coroutinemethod:: wait()
Victor Stinnerea3183f2013-12-03 01:08:00 +0100146
Yury Selivanov8be876e2018-09-11 17:10:37 -0700147 Wait until the event is set.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100148
Yury Selivanov8be876e2018-09-11 17:10:37 -0700149 If the event is set, return ``True`` immediately.
Matt Fowlerd90ff372020-12-23 05:44:52 -0500150 Otherwise block until another task calls :meth:`~Event.set`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100151
Yury Selivanov8be876e2018-09-11 17:10:37 -0700152 .. method:: set()
153
154 Set the event.
155
156 All tasks waiting for event to be set will be immediately
157 awakened.
158
159 .. method:: clear()
160
161 Clear (unset) the event.
162
Matt Fowlerd90ff372020-12-23 05:44:52 -0500163 Tasks awaiting on :meth:`~Event.wait` will now block until the
164 :meth:`~Event.set` method is called again.
Yury Selivanov8be876e2018-09-11 17:10:37 -0700165
166 .. method:: is_set()
167
168 Return ``True`` if the event is set.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100169
170
Victor Stinner8c462c52014-01-24 18:11:43 +0100171Condition
Yury Selivanov8be876e2018-09-11 17:10:37 -0700172=========
Victor Stinner8c462c52014-01-24 18:11:43 +0100173
Yurii Karabas86150d32020-11-29 14:50:57 +0200174.. class:: Condition(lock=None)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100175
Yury Selivanov8be876e2018-09-11 17:10:37 -0700176 A Condition object. Not thread-safe.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100177
Yury Selivanov8be876e2018-09-11 17:10:37 -0700178 An asyncio condition primitive can be used by a task to wait for
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400179 some event to happen and then get exclusive access to a shared
Yury Selivanov8be876e2018-09-11 17:10:37 -0700180 resource.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100181
Yury Selivanov8be876e2018-09-11 17:10:37 -0700182 In essence, a Condition object combines the functionality
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400183 of an :class:`Event` and a :class:`Lock`. It is possible to have
184 multiple Condition objects share one Lock, which allows coordinating
Yury Selivanov8be876e2018-09-11 17:10:37 -0700185 exclusive access to a shared resource between different tasks
186 interested in particular states of that shared resource.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100187
Yury Selivanov8be876e2018-09-11 17:10:37 -0700188 The optional *lock* argument must be a :class:`Lock` object or
189 ``None``. In the latter case a new Lock object is created
190 automatically.
Andrew Svetlov28d8d142017-12-09 20:00:05 +0200191
Miss Islington (bot)150a8e82021-05-26 15:19:42 -0700192 .. deprecated-removed:: 3.8 3.10
193 The ``loop`` parameter. This class has been implicitly getting the
194 current running loop since 3.7. See
195 :ref:`What's New in 3.10's Removed section <whatsnew310-removed>`
196 for more information.
197
Yury Selivanov8be876e2018-09-11 17:10:37 -0700198 The preferred way to use a Condition is an :keyword:`async with`
199 statement::
200
201 cond = asyncio.Condition()
202
203 # ... later
204 async with cond:
205 await cond.wait()
206
207 which is equivalent to::
208
209 cond = asyncio.Condition()
210
211 # ... later
Kevin Mai-Husan Chiad73ac0e2019-02-14 10:39:25 +0800212 await cond.acquire()
Yury Selivanov8be876e2018-09-11 17:10:37 -0700213 try:
214 await cond.wait()
215 finally:
Kevin Mai-Husan Chiad73ac0e2019-02-14 10:39:25 +0800216 cond.release()
Victor Stinner7b2c3c62015-02-26 10:39:16 +0100217
Victor Stinnerbdd574d2015-02-12 22:49:18 +0100218 .. coroutinemethod:: acquire()
Larry Hastings3732ed22014-03-15 21:13:56 -0700219
220 Acquire the underlying lock.
221
Yury Selivanov8be876e2018-09-11 17:10:37 -0700222 This method waits until the underlying lock is *unlocked*,
223 sets it to *locked* and returns ``True``.
Larry Hastings3732ed22014-03-15 21:13:56 -0700224
Victor Stinnerea3183f2013-12-03 01:08:00 +0100225 .. method:: notify(n=1)
226
Yury Selivanov8be876e2018-09-11 17:10:37 -0700227 Wake up at most *n* tasks (1 by default) waiting on this
228 condition. The method is no-op if no tasks are waiting.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100229
Yury Selivanov8be876e2018-09-11 17:10:37 -0700230 The lock must be acquired before this method is called and
231 released shortly after. If called with an *unlocked* lock
232 a :exc:`RuntimeError` error is raised.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100233
Larry Hastings3732ed22014-03-15 21:13:56 -0700234 .. method:: locked()
235
236 Return ``True`` if the underlying lock is acquired.
237
Victor Stinnerea3183f2013-12-03 01:08:00 +0100238 .. method:: notify_all()
239
Yury Selivanov8be876e2018-09-11 17:10:37 -0700240 Wake up all tasks waiting on this condition.
241
242 This method acts like :meth:`notify`, but wakes up all waiting
243 tasks.
244
245 The lock must be acquired before this method is called and
246 released shortly after. If called with an *unlocked* lock
247 a :exc:`RuntimeError` error is raised.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100248
Larry Hastings3732ed22014-03-15 21:13:56 -0700249 .. method:: release()
250
251 Release the underlying lock.
252
Yury Selivanov8be876e2018-09-11 17:10:37 -0700253 When invoked on an unlocked lock, a :exc:`RuntimeError` is
254 raised.
Larry Hastings3732ed22014-03-15 21:13:56 -0700255
Victor Stinnerbdd574d2015-02-12 22:49:18 +0100256 .. coroutinemethod:: wait()
Victor Stinnerea3183f2013-12-03 01:08:00 +0100257
258 Wait until notified.
259
Yury Selivanov8be876e2018-09-11 17:10:37 -0700260 If the calling task has not acquired the lock when this method is
Victor Stinnerea3183f2013-12-03 01:08:00 +0100261 called, a :exc:`RuntimeError` is raised.
262
Yury Selivanov8be876e2018-09-11 17:10:37 -0700263 This method releases the underlying lock, and then blocks until
264 it is awakened by a :meth:`notify` or :meth:`notify_all` call.
265 Once awakened, the Condition re-acquires its lock and this method
266 returns ``True``.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100267
Victor Stinnerbdd574d2015-02-12 22:49:18 +0100268 .. coroutinemethod:: wait_for(predicate)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100269
Yury Selivanov8be876e2018-09-11 17:10:37 -0700270 Wait until a predicate becomes *true*.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100271
Yury Selivanov8be876e2018-09-11 17:10:37 -0700272 The predicate must be a callable which result will be
273 interpreted as a boolean value. The final value is the
274 return value.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100275
276
Victor Stinner8c462c52014-01-24 18:11:43 +0100277Semaphore
Yury Selivanov8be876e2018-09-11 17:10:37 -0700278=========
Victor Stinner8c462c52014-01-24 18:11:43 +0100279
Yurii Karabas86150d32020-11-29 14:50:57 +0200280.. class:: Semaphore(value=1)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100281
Yury Selivanov8be876e2018-09-11 17:10:37 -0700282 A Semaphore object. Not thread-safe.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100283
284 A semaphore manages an internal counter which is decremented by each
Yury Selivanov8be876e2018-09-11 17:10:37 -0700285 :meth:`acquire` call and incremented by each :meth:`release` call.
286 The counter can never go below zero; when :meth:`acquire` finds
287 that it is zero, it blocks, waiting until some task calls
288 :meth:`release`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100289
Yury Selivanov8be876e2018-09-11 17:10:37 -0700290 The optional *value* argument gives the initial value for the
291 internal counter (``1`` by default). If the given value is
292 less than ``0`` a :exc:`ValueError` is raised.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100293
Miss Islington (bot)150a8e82021-05-26 15:19:42 -0700294 .. deprecated-removed:: 3.8 3.10
295 The ``loop`` parameter. This class has been implicitly getting the
296 current running loop since 3.7. See
297 :ref:`What's New in 3.10's Removed section <whatsnew310-removed>`
298 for more information.
299
Yury Selivanov8be876e2018-09-11 17:10:37 -0700300 The preferred way to use a Semaphore is an :keyword:`async with`
301 statement::
Andrew Svetlov28d8d142017-12-09 20:00:05 +0200302
Yury Selivanov8be876e2018-09-11 17:10:37 -0700303 sem = asyncio.Semaphore(10)
304
305 # ... later
306 async with sem:
307 # work with shared resource
308
309 which is equivalent to::
310
311 sem = asyncio.Semaphore(10)
312
313 # ... later
314 await sem.acquire()
315 try:
316 # work with shared resource
317 finally:
318 sem.release()
Victor Stinner7b2c3c62015-02-26 10:39:16 +0100319
Victor Stinnerbdd574d2015-02-12 22:49:18 +0100320 .. coroutinemethod:: acquire()
Victor Stinnerea3183f2013-12-03 01:08:00 +0100321
322 Acquire a semaphore.
323
Yury Selivanov8be876e2018-09-11 17:10:37 -0700324 If the internal counter is greater than zero, decrement
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400325 it by one and return ``True`` immediately. If it is zero, wait
Yury Selivanov8be876e2018-09-11 17:10:37 -0700326 until a :meth:`release` is called and return ``True``.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100327
328 .. method:: locked()
329
330 Returns ``True`` if semaphore can not be acquired immediately.
331
Andrew Svetlovb79e0122015-05-08 14:13:41 +0300332 .. method:: release()
Victor Stinnerea3183f2013-12-03 01:08:00 +0100333
Yury Selivanov8be876e2018-09-11 17:10:37 -0700334 Release a semaphore, incrementing the internal counter by one.
335 Can wake up a task waiting to acquire the semaphore.
336
337 Unlike :class:`BoundedSemaphore`, :class:`Semaphore` allows
Elvis Pranskevichus1fa2ec42018-09-17 19:16:44 -0400338 making more ``release()`` calls than ``acquire()`` calls.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100339
340
Victor Stinner8c462c52014-01-24 18:11:43 +0100341BoundedSemaphore
Yury Selivanov8be876e2018-09-11 17:10:37 -0700342================
Victor Stinner8c462c52014-01-24 18:11:43 +0100343
Yurii Karabas86150d32020-11-29 14:50:57 +0200344.. class:: BoundedSemaphore(value=1)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100345
Yury Selivanov8be876e2018-09-11 17:10:37 -0700346 A bounded semaphore object. Not thread-safe.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100347
Yury Selivanov8be876e2018-09-11 17:10:37 -0700348 Bounded Semaphore is a version of :class:`Semaphore` that raises
349 a :exc:`ValueError` in :meth:`~Semaphore.release` if it
350 increases the internal counter above the initial *value*.
Andrew Svetlov28d8d142017-12-09 20:00:05 +0200351
Miss Islington (bot)150a8e82021-05-26 15:19:42 -0700352 .. deprecated-removed:: 3.8 3.10
353
354 The ``loop`` parameter. This class has been implicitly getting the
355 current running loop since 3.7. See
356 :ref:`What's New in 3.10's Removed section <whatsnew310-removed>`
357 for more information.
358
Yury Selivanov8be876e2018-09-11 17:10:37 -0700359---------
Andrew Svetlov28d8d142017-12-09 20:00:05 +0200360
Andrew Svetlov28d8d142017-12-09 20:00:05 +0200361
Andrew Svetlov90d9ba62020-02-01 13:12:52 +0200362.. versionchanged:: 3.9
Andrew Svetlov28d8d142017-12-09 20:00:05 +0200363
Yury Selivanov8be876e2018-09-11 17:10:37 -0700364 Acquiring a lock using ``await lock`` or ``yield from lock`` and/or
Andrew Svetlov28d8d142017-12-09 20:00:05 +0200365 :keyword:`with` statement (``with await lock``, ``with (yield from
Andrew Svetlov90d9ba62020-02-01 13:12:52 +0200366 lock)``) was removed. Use ``async with lock`` instead.