blob: 8da5aa81ab9e403c60192c6def7c6e9296405f84 [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
Yury Selivanov8be876e2018-09-11 17:10:37 -07009asyncio synchronization primitives are designed to be similar to
10those of the :mod:`threading` module with two important caveats:
lf627d2c82017-07-25 17:03:51 -060011
Yury Selivanov8be876e2018-09-11 17:10:37 -070012* asyncio primitives are not thread-safe, therefore they should not
13 be used for OS threads synchronization (use :mod:`threading` for
14 that);
15
Carol Willing4e824e92018-09-13 18:28:19 -070016* methods of synchronization primitives do not accept the *timeout*
Yury Selivanov8be876e2018-09-11 17:10:37 -070017 argument; use the :func:`asyncio.wait_for` function to perform
18 operations with timeouts.
19
Carol Willing4e824e92018-09-13 18:28:19 -070020asyncio has the following basic sychronization primitives:
Victor Stinner7a55b882015-01-30 00:37:04 +010021
22* :class:`Lock`
23* :class:`Event`
24* :class:`Condition`
25* :class:`Semaphore`
26* :class:`BoundedSemaphore`
27
Yury Selivanov8be876e2018-09-11 17:10:37 -070028
29---------
Victor Stinner7a55b882015-01-30 00:37:04 +010030
Victor Stinnerea3183f2013-12-03 01:08:00 +010031
Victor Stinner8c462c52014-01-24 18:11:43 +010032Lock
Yury Selivanov8be876e2018-09-11 17:10:37 -070033====
Victor Stinner8c462c52014-01-24 18:11:43 +010034
Victor Stinnerea3183f2013-12-03 01:08:00 +010035.. class:: Lock(\*, loop=None)
36
Yury Selivanov8be876e2018-09-11 17:10:37 -070037 Implements a mutex lock for asyncio tasks. Not thread-safe.
Victor Stinnerea3183f2013-12-03 01:08:00 +010038
Yury Selivanov8be876e2018-09-11 17:10:37 -070039 An asyncio lock can be used to guarantee exclusive access to a
40 shared resource.
Victor Stinnerea3183f2013-12-03 01:08:00 +010041
Yury Selivanov8be876e2018-09-11 17:10:37 -070042 The preferred way to use a Lock is an :keyword:`async with`
43 statement::
Victor Stinnerea3183f2013-12-03 01:08:00 +010044
Yury Selivanov8be876e2018-09-11 17:10:37 -070045 lock = asyncio.Lock()
Victor Stinnerea3183f2013-12-03 01:08:00 +010046
Yury Selivanov8be876e2018-09-11 17:10:37 -070047 # ... later
48 async with lock:
49 # access shared state
Victor Stinnerea3183f2013-12-03 01:08:00 +010050
Yury Selivanov8be876e2018-09-11 17:10:37 -070051 which is equivalent to::
Victor Stinnerea3183f2013-12-03 01:08:00 +010052
Yury Selivanov8be876e2018-09-11 17:10:37 -070053 lock = asyncio.Lock()
Victor Stinner7b2c3c62015-02-26 10:39:16 +010054
Yury Selivanov8be876e2018-09-11 17:10:37 -070055 # ... later
56 await lock.acquire()
57 try:
58 # access shared state
59 finally:
60 lock.release()
Victor Stinnerea3183f2013-12-03 01:08:00 +010061
Victor Stinnerbdd574d2015-02-12 22:49:18 +010062 .. coroutinemethod:: acquire()
Victor Stinnerea3183f2013-12-03 01:08:00 +010063
Yury Selivanov8be876e2018-09-11 17:10:37 -070064 Acquire the lock.
Victor Stinnerea3183f2013-12-03 01:08:00 +010065
Yury Selivanov8be876e2018-09-11 17:10:37 -070066 This method waits until the lock is *unlocked*, sets it to
67 *locked* and returns ``True``.
Victor Stinnerea3183f2013-12-03 01:08:00 +010068
69 .. method:: release()
70
Yury Selivanov8be876e2018-09-11 17:10:37 -070071 Release the lock.
Victor Stinnerea3183f2013-12-03 01:08:00 +010072
Yury Selivanov8be876e2018-09-11 17:10:37 -070073 When the lock is *locked*, reset it to *unlocked* and return.
Victor Stinnerea3183f2013-12-03 01:08:00 +010074
Carol Willing4e824e92018-09-13 18:28:19 -070075 If the lock is *unlocked*, a :exc:`RuntimeError` is raised.
Victor Stinnerea3183f2013-12-03 01:08:00 +010076
Yury Selivanov8be876e2018-09-11 17:10:37 -070077 .. method:: locked()
78
79 Return ``True`` if the lock is *locked*.
Victor Stinnerea3183f2013-12-03 01:08:00 +010080
81
Victor Stinner8c462c52014-01-24 18:11:43 +010082Event
Yury Selivanov8be876e2018-09-11 17:10:37 -070083=====
Victor Stinner8c462c52014-01-24 18:11:43 +010084
Victor Stinnerea3183f2013-12-03 01:08:00 +010085.. class:: Event(\*, loop=None)
86
Yury Selivanov8be876e2018-09-11 17:10:37 -070087 An event object. Not thread-safe.
Victor Stinnerea3183f2013-12-03 01:08:00 +010088
Yury Selivanov8be876e2018-09-11 17:10:37 -070089 An asyncio event can be used to notify multiple asyncio tasks
90 that some event has happened.
Victor Stinnerea3183f2013-12-03 01:08:00 +010091
Yury Selivanov8be876e2018-09-11 17:10:37 -070092 An Event object manages an internal flag that can be set to *true*
93 with the :meth:`set` method and reset to *false* with the
94 :meth:`clear` method. The :meth:`wait` method blocks until the
95 flag is set to *true*. The flag is set to *false* initially.
Victor Stinner7b2c3c62015-02-26 10:39:16 +010096
Yury Selivanov8be876e2018-09-11 17:10:37 -070097 Example::
Victor Stinnerea3183f2013-12-03 01:08:00 +010098
Yury Selivanov8be876e2018-09-11 17:10:37 -070099 async def waiter(event):
Carol Willing4e824e92018-09-13 18:28:19 -0700100 print('waiting for it ...')
Yury Selivanov8be876e2018-09-11 17:10:37 -0700101 await event.wait()
102 print('... got it!')
Victor Stinnerea3183f2013-12-03 01:08:00 +0100103
Yury Selivanov8be876e2018-09-11 17:10:37 -0700104 async def main():
105 # Create an Event object.
106 event = asyncio.Event()
Victor Stinnerea3183f2013-12-03 01:08:00 +0100107
Yury Selivanov8be876e2018-09-11 17:10:37 -0700108 # Spawn a Task to wait until 'event' is set.
109 waiter_task = asyncio.create_task(waiter(event))
Victor Stinnerea3183f2013-12-03 01:08:00 +0100110
Yury Selivanov8be876e2018-09-11 17:10:37 -0700111 # Sleep for 1 second and set the event.
112 await asyncio.sleep(1)
113 event.set()
Victor Stinnerea3183f2013-12-03 01:08:00 +0100114
Yury Selivanov8be876e2018-09-11 17:10:37 -0700115 # Wait until the waiter task is finished.
116 await waiter_task
117
118 asyncio.run(main())
Victor Stinnerea3183f2013-12-03 01:08:00 +0100119
Victor Stinnerbdd574d2015-02-12 22:49:18 +0100120 .. coroutinemethod:: wait()
Victor Stinnerea3183f2013-12-03 01:08:00 +0100121
Yury Selivanov8be876e2018-09-11 17:10:37 -0700122 Wait until the event is set.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100123
Yury Selivanov8be876e2018-09-11 17:10:37 -0700124 If the event is set, return ``True`` immediately.
125 Otherwise block until another task calls :meth:`set`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100126
Yury Selivanov8be876e2018-09-11 17:10:37 -0700127 .. method:: set()
128
129 Set the event.
130
131 All tasks waiting for event to be set will be immediately
132 awakened.
133
134 .. method:: clear()
135
136 Clear (unset) the event.
137
138 Tasks awaiting on :meth:`wait` will now block until the
139 :meth:`set` method is called again.
140
141 .. method:: is_set()
142
143 Return ``True`` if the event is set.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100144
145
Victor Stinner8c462c52014-01-24 18:11:43 +0100146Condition
Yury Selivanov8be876e2018-09-11 17:10:37 -0700147=========
Victor Stinner8c462c52014-01-24 18:11:43 +0100148
Andrew Svetlovf200ce62014-07-26 19:50:37 +0300149.. class:: Condition(lock=None, \*, loop=None)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100150
Yury Selivanov8be876e2018-09-11 17:10:37 -0700151 A Condition object. Not thread-safe.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100152
Yury Selivanov8be876e2018-09-11 17:10:37 -0700153 An asyncio condition primitive can be used by a task to wait for
154 some event to happen and then get an exclusive access to a shared
155 resource.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100156
Yury Selivanov8be876e2018-09-11 17:10:37 -0700157 In essence, a Condition object combines the functionality
158 of :class:`Event` and :class:`Lock`. It is possible to have many
159 Condition objects sharing one Lock, which allows to coordinate
160 exclusive access to a shared resource between different tasks
161 interested in particular states of that shared resource.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100162
Yury Selivanov8be876e2018-09-11 17:10:37 -0700163 The optional *lock* argument must be a :class:`Lock` object or
164 ``None``. In the latter case a new Lock object is created
165 automatically.
Andrew Svetlov28d8d142017-12-09 20:00:05 +0200166
Yury Selivanov8be876e2018-09-11 17:10:37 -0700167 The preferred way to use a Condition is an :keyword:`async with`
168 statement::
169
170 cond = asyncio.Condition()
171
172 # ... later
173 async with cond:
174 await cond.wait()
175
176 which is equivalent to::
177
178 cond = asyncio.Condition()
179
180 # ... later
181 await lock.acquire()
182 try:
183 await cond.wait()
184 finally:
185 lock.release()
Victor Stinner7b2c3c62015-02-26 10:39:16 +0100186
Victor Stinnerbdd574d2015-02-12 22:49:18 +0100187 .. coroutinemethod:: acquire()
Larry Hastings3732ed22014-03-15 21:13:56 -0700188
189 Acquire the underlying lock.
190
Yury Selivanov8be876e2018-09-11 17:10:37 -0700191 This method waits until the underlying lock is *unlocked*,
192 sets it to *locked* and returns ``True``.
Larry Hastings3732ed22014-03-15 21:13:56 -0700193
Victor Stinnerea3183f2013-12-03 01:08:00 +0100194 .. method:: notify(n=1)
195
Yury Selivanov8be876e2018-09-11 17:10:37 -0700196 Wake up at most *n* tasks (1 by default) waiting on this
197 condition. The method is no-op if no tasks are waiting.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100198
Yury Selivanov8be876e2018-09-11 17:10:37 -0700199 The lock must be acquired before this method is called and
200 released shortly after. If called with an *unlocked* lock
201 a :exc:`RuntimeError` error is raised.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100202
Larry Hastings3732ed22014-03-15 21:13:56 -0700203 .. method:: locked()
204
205 Return ``True`` if the underlying lock is acquired.
206
Victor Stinnerea3183f2013-12-03 01:08:00 +0100207 .. method:: notify_all()
208
Yury Selivanov8be876e2018-09-11 17:10:37 -0700209 Wake up all tasks waiting on this condition.
210
211 This method acts like :meth:`notify`, but wakes up all waiting
212 tasks.
213
214 The lock must be acquired before this method is called and
215 released shortly after. If called with an *unlocked* lock
216 a :exc:`RuntimeError` error is raised.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100217
Larry Hastings3732ed22014-03-15 21:13:56 -0700218 .. method:: release()
219
220 Release the underlying lock.
221
Yury Selivanov8be876e2018-09-11 17:10:37 -0700222 When invoked on an unlocked lock, a :exc:`RuntimeError` is
223 raised.
Larry Hastings3732ed22014-03-15 21:13:56 -0700224
Victor Stinnerbdd574d2015-02-12 22:49:18 +0100225 .. coroutinemethod:: wait()
Victor Stinnerea3183f2013-12-03 01:08:00 +0100226
227 Wait until notified.
228
Yury Selivanov8be876e2018-09-11 17:10:37 -0700229 If the calling task has not acquired the lock when this method is
Victor Stinnerea3183f2013-12-03 01:08:00 +0100230 called, a :exc:`RuntimeError` is raised.
231
Yury Selivanov8be876e2018-09-11 17:10:37 -0700232 This method releases the underlying lock, and then blocks until
233 it is awakened by a :meth:`notify` or :meth:`notify_all` call.
234 Once awakened, the Condition re-acquires its lock and this method
235 returns ``True``.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100236
Victor Stinnerbdd574d2015-02-12 22:49:18 +0100237 .. coroutinemethod:: wait_for(predicate)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100238
Yury Selivanov8be876e2018-09-11 17:10:37 -0700239 Wait until a predicate becomes *true*.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100240
Yury Selivanov8be876e2018-09-11 17:10:37 -0700241 The predicate must be a callable which result will be
242 interpreted as a boolean value. The final value is the
243 return value.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100244
245
Victor Stinner8c462c52014-01-24 18:11:43 +0100246Semaphore
Yury Selivanov8be876e2018-09-11 17:10:37 -0700247=========
Victor Stinner8c462c52014-01-24 18:11:43 +0100248
Victor Stinnerea3183f2013-12-03 01:08:00 +0100249.. class:: Semaphore(value=1, \*, loop=None)
250
Yury Selivanov8be876e2018-09-11 17:10:37 -0700251 A Semaphore object. Not thread-safe.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100252
253 A semaphore manages an internal counter which is decremented by each
Yury Selivanov8be876e2018-09-11 17:10:37 -0700254 :meth:`acquire` call and incremented by each :meth:`release` call.
255 The counter can never go below zero; when :meth:`acquire` finds
256 that it is zero, it blocks, waiting until some task calls
257 :meth:`release`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100258
Yury Selivanov8be876e2018-09-11 17:10:37 -0700259 The optional *value* argument gives the initial value for the
260 internal counter (``1`` by default). If the given value is
261 less than ``0`` a :exc:`ValueError` is raised.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100262
Yury Selivanov8be876e2018-09-11 17:10:37 -0700263 The preferred way to use a Semaphore is an :keyword:`async with`
264 statement::
Andrew Svetlov28d8d142017-12-09 20:00:05 +0200265
Yury Selivanov8be876e2018-09-11 17:10:37 -0700266 sem = asyncio.Semaphore(10)
267
268 # ... later
269 async with sem:
270 # work with shared resource
271
272 which is equivalent to::
273
274 sem = asyncio.Semaphore(10)
275
276 # ... later
277 await sem.acquire()
278 try:
279 # work with shared resource
280 finally:
281 sem.release()
Victor Stinner7b2c3c62015-02-26 10:39:16 +0100282
Victor Stinnerbdd574d2015-02-12 22:49:18 +0100283 .. coroutinemethod:: acquire()
Victor Stinnerea3183f2013-12-03 01:08:00 +0100284
285 Acquire a semaphore.
286
Yury Selivanov8be876e2018-09-11 17:10:37 -0700287 If the internal counter is greater than zero, decrement
288 it by one and return ``True`` immediately. If it is zero wait
289 until a :meth:`release` is called and return ``True``.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100290
291 .. method:: locked()
292
293 Returns ``True`` if semaphore can not be acquired immediately.
294
Andrew Svetlovb79e0122015-05-08 14:13:41 +0300295 .. method:: release()
Victor Stinnerea3183f2013-12-03 01:08:00 +0100296
Yury Selivanov8be876e2018-09-11 17:10:37 -0700297 Release a semaphore, incrementing the internal counter by one.
298 Can wake up a task waiting to acquire the semaphore.
299
300 Unlike :class:`BoundedSemaphore`, :class:`Semaphore` allows
301 to make more ``release()`` calls than ``acquire()`` calls.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100302
303
Victor Stinner8c462c52014-01-24 18:11:43 +0100304BoundedSemaphore
Yury Selivanov8be876e2018-09-11 17:10:37 -0700305================
Victor Stinner8c462c52014-01-24 18:11:43 +0100306
Victor Stinnerea3183f2013-12-03 01:08:00 +0100307.. class:: BoundedSemaphore(value=1, \*, loop=None)
308
Yury Selivanov8be876e2018-09-11 17:10:37 -0700309 A bounded semaphore object. Not thread-safe.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100310
Yury Selivanov8be876e2018-09-11 17:10:37 -0700311 Bounded Semaphore is a version of :class:`Semaphore` that raises
312 a :exc:`ValueError` in :meth:`~Semaphore.release` if it
313 increases the internal counter above the initial *value*.
Andrew Svetlov28d8d142017-12-09 20:00:05 +0200314
315
Yury Selivanov8be876e2018-09-11 17:10:37 -0700316---------
Andrew Svetlov28d8d142017-12-09 20:00:05 +0200317
Andrew Svetlov28d8d142017-12-09 20:00:05 +0200318
319.. deprecated:: 3.7
320
Yury Selivanov8be876e2018-09-11 17:10:37 -0700321 Acquiring a lock using ``await lock`` or ``yield from lock`` and/or
Andrew Svetlov28d8d142017-12-09 20:00:05 +0200322 :keyword:`with` statement (``with await lock``, ``with (yield from
Yury Selivanov8be876e2018-09-11 17:10:37 -0700323 lock)``) is deprecated. Use ``async with lock`` instead.