blob: 14e3defbf4118a68f6d9cb6a0fb0e8fa085266cc [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 +010026Locks
27-----
28
Victor Stinner8c462c52014-01-24 18:11:43 +010029Lock
30^^^^
31
Victor Stinnerea3183f2013-12-03 01:08:00 +010032.. class:: Lock(\*, loop=None)
33
34 Primitive lock objects.
35
36 A primitive lock is a synchronization primitive that is not owned by a
37 particular coroutine when locked. A primitive lock is in one of two states,
38 'locked' or 'unlocked'.
39
40 It is created in the unlocked state. It has two basic methods, :meth:`acquire`
41 and :meth:`release`. When the state is unlocked, acquire() changes the state to
42 locked and returns immediately. When the state is locked, acquire() blocks
43 until a call to release() in another coroutine changes it to unlocked, then
44 the acquire() call resets it to locked and returns. The release() method
45 should only be called in the locked state; it changes the state to unlocked
46 and returns immediately. If an attempt is made to release an unlocked lock,
47 a :exc:`RuntimeError` will be raised.
48
49 When more than one coroutine is blocked in acquire() waiting for the state
50 to turn to unlocked, only one coroutine proceeds when a release() call
51 resets the state to unlocked; first coroutine which is blocked in acquire()
52 is being processed.
53
54 :meth:`acquire` is a coroutine and should be called with ``yield from``.
55
Serhiy Storchaka14867992014-09-10 23:43:41 +030056 Locks also support the context management protocol. ``(yield from lock)``
Martin Panter3ee62702016-06-04 04:57:19 +000057 should be used as the context manager expression.
Victor Stinnerea3183f2013-12-03 01:08:00 +010058
Victor Stinner7b2c3c62015-02-26 10:39:16 +010059 This class is :ref:`not thread safe <asyncio-multithreading>`.
60
Victor Stinnerea3183f2013-12-03 01:08:00 +010061 Usage::
62
63 lock = Lock()
64 ...
65 yield from lock
66 try:
67 ...
68 finally:
69 lock.release()
70
71 Context manager usage::
72
73 lock = Lock()
74 ...
75 with (yield from lock):
Serhiy Storchakadba90392016-05-10 12:01:23 +030076 ...
Victor Stinnerea3183f2013-12-03 01:08:00 +010077
78 Lock objects can be tested for locking state::
79
80 if not lock.locked():
Serhiy Storchakadba90392016-05-10 12:01:23 +030081 yield from lock
Victor Stinnerea3183f2013-12-03 01:08:00 +010082 else:
Serhiy Storchakadba90392016-05-10 12:01:23 +030083 # lock is acquired
Victor Stinnerea3183f2013-12-03 01:08:00 +010084 ...
85
86 .. method:: locked()
87
Larry Hastings3732ed22014-03-15 21:13:56 -070088 Return ``True`` if the lock is acquired.
Victor Stinnerea3183f2013-12-03 01:08:00 +010089
Victor Stinnerbdd574d2015-02-12 22:49:18 +010090 .. coroutinemethod:: acquire()
Victor Stinnerea3183f2013-12-03 01:08:00 +010091
92 Acquire a lock.
93
94 This method blocks until the lock is unlocked, then sets it to locked and
95 returns ``True``.
96
Larry Hastings3732ed22014-03-15 21:13:56 -070097 This method is a :ref:`coroutine <coroutine>`.
Victor Stinnerea3183f2013-12-03 01:08:00 +010098
99 .. method:: release()
100
101 Release a lock.
102
103 When the lock is locked, reset it to unlocked, and return. If any other
104 coroutines are blocked waiting for the lock to become unlocked, allow
105 exactly one of them to proceed.
106
107 When invoked on an unlocked lock, a :exc:`RuntimeError` is raised.
108
109 There is no return value.
110
111
Victor Stinner8c462c52014-01-24 18:11:43 +0100112Event
113^^^^^
114
Victor Stinnerea3183f2013-12-03 01:08:00 +0100115.. class:: Event(\*, loop=None)
116
117 An Event implementation, asynchronous equivalent to :class:`threading.Event`.
118
119 Class implementing event objects. An event manages a flag that can be set to
120 true with the :meth:`set` method and reset to false with the :meth:`clear`
121 method. The :meth:`wait` method blocks until the flag is true. The flag is
122 initially false.
123
Victor Stinner7b2c3c62015-02-26 10:39:16 +0100124 This class is :ref:`not thread safe <asyncio-multithreading>`.
125
Victor Stinnerea3183f2013-12-03 01:08:00 +0100126 .. method:: clear()
127
128 Reset the internal flag to false. Subsequently, coroutines calling
129 :meth:`wait` will block until :meth:`set` is called to set the internal
130 flag to true again.
131
132 .. method:: is_set()
133
134 Return ``True`` if and only if the internal flag is true.
135
136 .. method:: set()
137
138 Set the internal flag to true. All coroutines waiting for it to become
139 true are awakened. Coroutine that call :meth:`wait` once the flag is true
140 will not block at all.
141
Victor Stinnerbdd574d2015-02-12 22:49:18 +0100142 .. coroutinemethod:: wait()
Victor Stinnerea3183f2013-12-03 01:08:00 +0100143
144 Block until the internal flag is true.
145
146 If the internal flag is true on entry, return ``True`` immediately.
147 Otherwise, block until another coroutine calls :meth:`set` to set the
148 flag to true, then return ``True``.
149
Yury Selivanov37f15bc2014-02-20 16:20:44 -0500150 This method is a :ref:`coroutine <coroutine>`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100151
152
Victor Stinner8c462c52014-01-24 18:11:43 +0100153Condition
154^^^^^^^^^
155
Andrew Svetlovf200ce62014-07-26 19:50:37 +0300156.. class:: Condition(lock=None, \*, loop=None)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100157
158 A Condition implementation, asynchronous equivalent to
159 :class:`threading.Condition`.
160
161 This class implements condition variable objects. A condition variable
162 allows one or more coroutines to wait until they are notified by another
163 coroutine.
164
Andrew Svetlovf200ce62014-07-26 19:50:37 +0300165 If the *lock* argument is given and not ``None``, it must be a :class:`Lock`
166 object, and it is used as the underlying lock. Otherwise,
167 a new :class:`Lock` object is created and used as the underlying lock.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100168
Victor Stinner7b2c3c62015-02-26 10:39:16 +0100169 This class is :ref:`not thread safe <asyncio-multithreading>`.
170
Victor Stinnerbdd574d2015-02-12 22:49:18 +0100171 .. coroutinemethod:: acquire()
Larry Hastings3732ed22014-03-15 21:13:56 -0700172
173 Acquire the underlying lock.
174
175 This method blocks until the lock is unlocked, then sets it to locked and
176 returns ``True``.
177
178 This method is a :ref:`coroutine <coroutine>`.
179
Victor Stinnerea3183f2013-12-03 01:08:00 +0100180 .. method:: notify(n=1)
181
182 By default, wake up one coroutine waiting on this condition, if any.
183 If the calling coroutine has not acquired the lock when this method is
184 called, a :exc:`RuntimeError` is raised.
185
186 This method wakes up at most *n* of the coroutines waiting for the
187 condition variable; it is a no-op if no coroutines are waiting.
188
189 .. note::
190
191 An awakened coroutine does not actually return from its :meth:`wait`
192 call until it can reacquire the lock. Since :meth:`notify` does not
193 release the lock, its caller should.
194
Larry Hastings3732ed22014-03-15 21:13:56 -0700195 .. method:: locked()
196
197 Return ``True`` if the underlying lock is acquired.
198
Victor Stinnerea3183f2013-12-03 01:08:00 +0100199 .. method:: notify_all()
200
Zachary Ware828946e2015-05-06 20:19:06 -0500201 Wake up all coroutines waiting on this condition. This method acts like
202 :meth:`notify`, but wakes up all waiting coroutines instead of one. If the
203 calling coroutine has not acquired the lock when this method is called, a
Victor Stinnerea3183f2013-12-03 01:08:00 +0100204 :exc:`RuntimeError` is raised.
205
Larry Hastings3732ed22014-03-15 21:13:56 -0700206 .. method:: release()
207
208 Release the underlying lock.
209
210 When the lock is locked, reset it to unlocked, and return. If any other
211 coroutines are blocked waiting for the lock to become unlocked, allow
212 exactly one of them to proceed.
213
214 When invoked on an unlocked lock, a :exc:`RuntimeError` is raised.
215
216 There is no return value.
217
Victor Stinnerbdd574d2015-02-12 22:49:18 +0100218 .. coroutinemethod:: wait()
Victor Stinnerea3183f2013-12-03 01:08:00 +0100219
220 Wait until notified.
221
222 If the calling coroutine has not acquired the lock when this method is
223 called, a :exc:`RuntimeError` is raised.
224
225 This method releases the underlying lock, and then blocks until it is
226 awakened by a :meth:`notify` or :meth:`notify_all` call for the same
227 condition variable in another coroutine. Once awakened, it re-acquires
228 the lock and returns ``True``.
229
Yury Selivanov37f15bc2014-02-20 16:20:44 -0500230 This method is a :ref:`coroutine <coroutine>`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100231
Victor Stinnerbdd574d2015-02-12 22:49:18 +0100232 .. coroutinemethod:: wait_for(predicate)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100233
234 Wait until a predicate becomes true.
235
236 The predicate should be a callable which result will be interpreted as a
237 boolean value. The final predicate value is the return value.
238
Yury Selivanov37f15bc2014-02-20 16:20:44 -0500239 This method is a :ref:`coroutine <coroutine>`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100240
241
242Semaphores
243----------
244
Victor Stinner8c462c52014-01-24 18:11:43 +0100245Semaphore
246^^^^^^^^^
247
Victor Stinnerea3183f2013-12-03 01:08:00 +0100248.. class:: Semaphore(value=1, \*, loop=None)
249
250 A Semaphore implementation.
251
252 A semaphore manages an internal counter which is decremented by each
253 :meth:`acquire` call and incremented by each :meth:`release` call. The
254 counter can never go below zero; when :meth:`acquire` finds that it is zero,
Zachary Ware828946e2015-05-06 20:19:06 -0500255 it blocks, waiting until some other coroutine calls :meth:`release`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100256
Serhiy Storchaka14867992014-09-10 23:43:41 +0300257 Semaphores also support the context management protocol.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100258
259 The optional argument gives the initial value for the internal counter; it
260 defaults to ``1``. If the value given is less than ``0``, :exc:`ValueError`
261 is raised.
262
Victor Stinner7b2c3c62015-02-26 10:39:16 +0100263 This class is :ref:`not thread safe <asyncio-multithreading>`.
264
Victor Stinnerbdd574d2015-02-12 22:49:18 +0100265 .. coroutinemethod:: acquire()
Victor Stinnerea3183f2013-12-03 01:08:00 +0100266
267 Acquire a semaphore.
268
269 If the internal counter is larger than zero on entry, decrement it by one
270 and return ``True`` immediately. If it is zero on entry, block, waiting
271 until some other coroutine has called :meth:`release` to make it larger
272 than ``0``, and then return ``True``.
273
Yury Selivanov37f15bc2014-02-20 16:20:44 -0500274 This method is a :ref:`coroutine <coroutine>`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100275
276 .. method:: locked()
277
278 Returns ``True`` if semaphore can not be acquired immediately.
279
Andrew Svetlovb79e0122015-05-08 14:13:41 +0300280 .. method:: release()
Victor Stinnerea3183f2013-12-03 01:08:00 +0100281
282 Release a semaphore, incrementing the internal counter by one. When it
283 was zero on entry and another coroutine is waiting for it to become
284 larger than zero again, wake up that coroutine.
285
286
Victor Stinner8c462c52014-01-24 18:11:43 +0100287BoundedSemaphore
288^^^^^^^^^^^^^^^^
289
Victor Stinnerea3183f2013-12-03 01:08:00 +0100290.. class:: BoundedSemaphore(value=1, \*, loop=None)
291
Victor Stinner7b2c3c62015-02-26 10:39:16 +0100292 A bounded semaphore implementation. Inherit from :class:`Semaphore`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100293
Victor Stinner7b2c3c62015-02-26 10:39:16 +0100294 This raises :exc:`ValueError` in :meth:`~Semaphore.release` if it would
295 increase the value above the initial value.