blob: 3e574f41e80f17e2acecd641cf19ac7c493ff703 [file] [log] [blame]
R David Murray6a143812013-12-20 14:37:39 -05001.. currentmodule:: asyncio
Victor Stinner606ab032014-02-01 03:18:58 +01002.. _asyncio-sync:
Victor Stinnerea3183f2013-12-03 01:08:00 +01003
4Synchronization primitives
5==========================
6
lf627d2c82017-07-25 17:03:51 -06007**Source code:** :source:`Lib/asyncio/locks.py`
8
Victor Stinner7a55b882015-01-30 00:37:04 +01009Locks:
10
11* :class:`Lock`
12* :class:`Event`
13* :class:`Condition`
Victor Stinner615a58e2015-02-25 13:55:43 +010014
15Semaphores:
16
Victor Stinner7a55b882015-01-30 00:37:04 +010017* :class:`Semaphore`
18* :class:`BoundedSemaphore`
19
Victor Stinner615a58e2015-02-25 13:55:43 +010020asyncio lock API was designed to be close to classes of the :mod:`threading`
21module (:class:`~threading.Lock`, :class:`~threading.Event`,
Victor Stinner7a55b882015-01-30 00:37:04 +010022:class:`~threading.Condition`, :class:`~threading.Semaphore`,
Victor Stinner615a58e2015-02-25 13:55:43 +010023:class:`~threading.BoundedSemaphore`), but it has no *timeout* parameter. The
Victor Stinner7a55b882015-01-30 00:37:04 +010024:func:`asyncio.wait_for` function can be used to cancel a task after a timeout.
25
Victor Stinnerea3183f2013-12-03 01:08:00 +010026
Victor Stinner8c462c52014-01-24 18:11:43 +010027Lock
Andrew Svetlov28d8d142017-12-09 20:00:05 +020028----
Victor Stinner8c462c52014-01-24 18:11:43 +010029
Victor Stinnerea3183f2013-12-03 01:08:00 +010030.. class:: Lock(\*, loop=None)
31
32 Primitive lock objects.
33
34 A primitive lock is a synchronization primitive that is not owned by a
35 particular coroutine when locked. A primitive lock is in one of two states,
36 'locked' or 'unlocked'.
37
Andrew Svetlov28d8d142017-12-09 20:00:05 +020038 The lock is created in the unlocked state.
39 It has two basic methods, :meth:`acquire` and :meth:`release`.
40 When the state is unlocked, acquire() changes the state to
Victor Stinnerea3183f2013-12-03 01:08:00 +010041 locked and returns immediately. When the state is locked, acquire() blocks
42 until a call to release() in another coroutine changes it to unlocked, then
43 the acquire() call resets it to locked and returns. The release() method
44 should only be called in the locked state; it changes the state to unlocked
45 and returns immediately. If an attempt is made to release an unlocked lock,
46 a :exc:`RuntimeError` will be raised.
47
48 When more than one coroutine is blocked in acquire() waiting for the state
49 to turn to unlocked, only one coroutine proceeds when a release() call
50 resets the state to unlocked; first coroutine which is blocked in acquire()
51 is being processed.
52
Andrew Svetlov28d8d142017-12-09 20:00:05 +020053 :meth:`acquire` is a coroutine and should be called with ``await``.
Victor Stinnerea3183f2013-12-03 01:08:00 +010054
Andrew Svetlov28d8d142017-12-09 20:00:05 +020055 Locks support the :ref:`context management protocol <async-with-locks>`.
Victor Stinnerea3183f2013-12-03 01:08:00 +010056
Victor Stinner7b2c3c62015-02-26 10:39:16 +010057 This class is :ref:`not thread safe <asyncio-multithreading>`.
58
Victor Stinnerea3183f2013-12-03 01:08:00 +010059 .. method:: locked()
60
Larry Hastings3732ed22014-03-15 21:13:56 -070061 Return ``True`` if the lock is acquired.
Victor Stinnerea3183f2013-12-03 01:08:00 +010062
Victor Stinnerbdd574d2015-02-12 22:49:18 +010063 .. coroutinemethod:: acquire()
Victor Stinnerea3183f2013-12-03 01:08:00 +010064
65 Acquire a lock.
66
67 This method blocks until the lock is unlocked, then sets it to locked and
68 returns ``True``.
69
Larry Hastings3732ed22014-03-15 21:13:56 -070070 This method is a :ref:`coroutine <coroutine>`.
Victor Stinnerea3183f2013-12-03 01:08:00 +010071
72 .. method:: release()
73
74 Release a lock.
75
76 When the lock is locked, reset it to unlocked, and return. If any other
77 coroutines are blocked waiting for the lock to become unlocked, allow
78 exactly one of them to proceed.
79
80 When invoked on an unlocked lock, a :exc:`RuntimeError` is raised.
81
82 There is no return value.
83
84
Victor Stinner8c462c52014-01-24 18:11:43 +010085Event
Andrew Svetlov28d8d142017-12-09 20:00:05 +020086-----
Victor Stinner8c462c52014-01-24 18:11:43 +010087
Victor Stinnerea3183f2013-12-03 01:08:00 +010088.. class:: Event(\*, loop=None)
89
90 An Event implementation, asynchronous equivalent to :class:`threading.Event`.
91
92 Class implementing event objects. An event manages a flag that can be set to
93 true with the :meth:`set` method and reset to false with the :meth:`clear`
94 method. The :meth:`wait` method blocks until the flag is true. The flag is
95 initially false.
96
Victor Stinner7b2c3c62015-02-26 10:39:16 +010097 This class is :ref:`not thread safe <asyncio-multithreading>`.
98
Victor Stinnerea3183f2013-12-03 01:08:00 +010099 .. method:: clear()
100
101 Reset the internal flag to false. Subsequently, coroutines calling
102 :meth:`wait` will block until :meth:`set` is called to set the internal
103 flag to true again.
104
105 .. method:: is_set()
106
107 Return ``True`` if and only if the internal flag is true.
108
109 .. method:: set()
110
111 Set the internal flag to true. All coroutines waiting for it to become
112 true are awakened. Coroutine that call :meth:`wait` once the flag is true
113 will not block at all.
114
Victor Stinnerbdd574d2015-02-12 22:49:18 +0100115 .. coroutinemethod:: wait()
Victor Stinnerea3183f2013-12-03 01:08:00 +0100116
117 Block until the internal flag is true.
118
119 If the internal flag is true on entry, return ``True`` immediately.
120 Otherwise, block until another coroutine calls :meth:`set` to set the
121 flag to true, then return ``True``.
122
Yury Selivanov37f15bc2014-02-20 16:20:44 -0500123 This method is a :ref:`coroutine <coroutine>`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100124
125
Victor Stinner8c462c52014-01-24 18:11:43 +0100126Condition
Andrew Svetlov28d8d142017-12-09 20:00:05 +0200127---------
Victor Stinner8c462c52014-01-24 18:11:43 +0100128
Andrew Svetlovf200ce62014-07-26 19:50:37 +0300129.. class:: Condition(lock=None, \*, loop=None)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100130
131 A Condition implementation, asynchronous equivalent to
132 :class:`threading.Condition`.
133
134 This class implements condition variable objects. A condition variable
135 allows one or more coroutines to wait until they are notified by another
136 coroutine.
137
Andrew Svetlovf200ce62014-07-26 19:50:37 +0300138 If the *lock* argument is given and not ``None``, it must be a :class:`Lock`
139 object, and it is used as the underlying lock. Otherwise,
140 a new :class:`Lock` object is created and used as the underlying lock.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100141
Andrew Svetlov28d8d142017-12-09 20:00:05 +0200142 Conditions support the :ref:`context management protocol
143 <async-with-locks>`.
144
Victor Stinner7b2c3c62015-02-26 10:39:16 +0100145 This class is :ref:`not thread safe <asyncio-multithreading>`.
146
Victor Stinnerbdd574d2015-02-12 22:49:18 +0100147 .. coroutinemethod:: acquire()
Larry Hastings3732ed22014-03-15 21:13:56 -0700148
149 Acquire the underlying lock.
150
151 This method blocks until the lock is unlocked, then sets it to locked and
152 returns ``True``.
153
154 This method is a :ref:`coroutine <coroutine>`.
155
Victor Stinnerea3183f2013-12-03 01:08:00 +0100156 .. method:: notify(n=1)
157
158 By default, wake up one coroutine waiting on this condition, if any.
159 If the calling coroutine has not acquired the lock when this method is
160 called, a :exc:`RuntimeError` is raised.
161
162 This method wakes up at most *n* of the coroutines waiting for the
163 condition variable; it is a no-op if no coroutines are waiting.
164
165 .. note::
166
167 An awakened coroutine does not actually return from its :meth:`wait`
168 call until it can reacquire the lock. Since :meth:`notify` does not
169 release the lock, its caller should.
170
Larry Hastings3732ed22014-03-15 21:13:56 -0700171 .. method:: locked()
172
173 Return ``True`` if the underlying lock is acquired.
174
Victor Stinnerea3183f2013-12-03 01:08:00 +0100175 .. method:: notify_all()
176
Zachary Ware828946e2015-05-06 20:19:06 -0500177 Wake up all coroutines waiting on this condition. This method acts like
178 :meth:`notify`, but wakes up all waiting coroutines instead of one. If the
179 calling coroutine has not acquired the lock when this method is called, a
Victor Stinnerea3183f2013-12-03 01:08:00 +0100180 :exc:`RuntimeError` is raised.
181
Larry Hastings3732ed22014-03-15 21:13:56 -0700182 .. method:: release()
183
184 Release the underlying lock.
185
186 When the lock is locked, reset it to unlocked, and return. If any other
187 coroutines are blocked waiting for the lock to become unlocked, allow
188 exactly one of them to proceed.
189
190 When invoked on an unlocked lock, a :exc:`RuntimeError` is raised.
191
192 There is no return value.
193
Victor Stinnerbdd574d2015-02-12 22:49:18 +0100194 .. coroutinemethod:: wait()
Victor Stinnerea3183f2013-12-03 01:08:00 +0100195
196 Wait until notified.
197
198 If the calling coroutine has not acquired the lock when this method is
199 called, a :exc:`RuntimeError` is raised.
200
201 This method releases the underlying lock, and then blocks until it is
202 awakened by a :meth:`notify` or :meth:`notify_all` call for the same
203 condition variable in another coroutine. Once awakened, it re-acquires
204 the lock and returns ``True``.
205
Yury Selivanov37f15bc2014-02-20 16:20:44 -0500206 This method is a :ref:`coroutine <coroutine>`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100207
Victor Stinnerbdd574d2015-02-12 22:49:18 +0100208 .. coroutinemethod:: wait_for(predicate)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100209
210 Wait until a predicate becomes true.
211
212 The predicate should be a callable which result will be interpreted as a
213 boolean value. The final predicate value is the return value.
214
Yury Selivanov37f15bc2014-02-20 16:20:44 -0500215 This method is a :ref:`coroutine <coroutine>`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100216
217
Victor Stinner8c462c52014-01-24 18:11:43 +0100218Semaphore
Andrew Svetlov28d8d142017-12-09 20:00:05 +0200219---------
Victor Stinner8c462c52014-01-24 18:11:43 +0100220
Victor Stinnerea3183f2013-12-03 01:08:00 +0100221.. class:: Semaphore(value=1, \*, loop=None)
222
223 A Semaphore implementation.
224
225 A semaphore manages an internal counter which is decremented by each
226 :meth:`acquire` call and incremented by each :meth:`release` call. The
227 counter can never go below zero; when :meth:`acquire` finds that it is zero,
Zachary Ware828946e2015-05-06 20:19:06 -0500228 it blocks, waiting until some other coroutine calls :meth:`release`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100229
Victor Stinnerea3183f2013-12-03 01:08:00 +0100230 The optional argument gives the initial value for the internal counter; it
231 defaults to ``1``. If the value given is less than ``0``, :exc:`ValueError`
232 is raised.
233
Andrew Svetlov28d8d142017-12-09 20:00:05 +0200234 Semaphores support the :ref:`context management protocol
235 <async-with-locks>`.
236
Victor Stinner7b2c3c62015-02-26 10:39:16 +0100237 This class is :ref:`not thread safe <asyncio-multithreading>`.
238
Victor Stinnerbdd574d2015-02-12 22:49:18 +0100239 .. coroutinemethod:: acquire()
Victor Stinnerea3183f2013-12-03 01:08:00 +0100240
241 Acquire a semaphore.
242
243 If the internal counter is larger than zero on entry, decrement it by one
244 and return ``True`` immediately. If it is zero on entry, block, waiting
245 until some other coroutine has called :meth:`release` to make it larger
246 than ``0``, and then return ``True``.
247
Yury Selivanov37f15bc2014-02-20 16:20:44 -0500248 This method is a :ref:`coroutine <coroutine>`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100249
250 .. method:: locked()
251
252 Returns ``True`` if semaphore can not be acquired immediately.
253
Andrew Svetlovb79e0122015-05-08 14:13:41 +0300254 .. method:: release()
Victor Stinnerea3183f2013-12-03 01:08:00 +0100255
256 Release a semaphore, incrementing the internal counter by one. When it
257 was zero on entry and another coroutine is waiting for it to become
258 larger than zero again, wake up that coroutine.
259
260
Victor Stinner8c462c52014-01-24 18:11:43 +0100261BoundedSemaphore
Andrew Svetlov28d8d142017-12-09 20:00:05 +0200262----------------
Victor Stinner8c462c52014-01-24 18:11:43 +0100263
Victor Stinnerea3183f2013-12-03 01:08:00 +0100264.. class:: BoundedSemaphore(value=1, \*, loop=None)
265
Victor Stinner7b2c3c62015-02-26 10:39:16 +0100266 A bounded semaphore implementation. Inherit from :class:`Semaphore`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100267
Victor Stinner7b2c3c62015-02-26 10:39:16 +0100268 This raises :exc:`ValueError` in :meth:`~Semaphore.release` if it would
269 increase the value above the initial value.
Andrew Svetlov28d8d142017-12-09 20:00:05 +0200270
271 Bounded semapthores support the :ref:`context management
272 protocol <async-with-locks>`.
273
274 This class is :ref:`not thread safe <asyncio-multithreading>`.
275
276
277.. _async-with-locks:
278
279Using locks, conditions and semaphores in the :keyword:`async with` statement
280-----------------------------------------------------------------------------
281
282:class:`Lock`, :class:`Condition`, :class:`Semaphore`, and
283:class:`BoundedSemaphore` objects can be used in :keyword:`async with`
284statements.
285
286The :meth:`acquire` method will be called when the block is entered,
287and :meth:`release` will be called when the block is exited. Hence,
288the following snippet::
289
290 async with lock:
291 # do something...
292
293is equivalent to::
294
295 await lock.acquire()
296 try:
297 # do something...
298 finally:
299 lock.release()
300
301.. deprecated:: 3.7
302
303 Lock acquiring using ``await lock`` or ``yield from lock`` and
304 :keyword:`with` statement (``with await lock``, ``with (yield from
305 lock)``) are deprecated.