blob: ad3b523f989b9758d00f24016dc2fb43c029f35e [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
Victor Stinner7a55b882015-01-30 00:37:04 +01007Locks:
8
9* :class:`Lock`
10* :class:`Event`
11* :class:`Condition`
Victor Stinner615a58e2015-02-25 13:55:43 +010012
13Semaphores:
14
Victor Stinner7a55b882015-01-30 00:37:04 +010015* :class:`Semaphore`
16* :class:`BoundedSemaphore`
17
Victor Stinner615a58e2015-02-25 13:55:43 +010018asyncio lock API was designed to be close to classes of the :mod:`threading`
19module (:class:`~threading.Lock`, :class:`~threading.Event`,
Victor Stinner7a55b882015-01-30 00:37:04 +010020:class:`~threading.Condition`, :class:`~threading.Semaphore`,
Victor Stinner615a58e2015-02-25 13:55:43 +010021:class:`~threading.BoundedSemaphore`), but it has no *timeout* parameter. The
Victor Stinner7a55b882015-01-30 00:37:04 +010022:func:`asyncio.wait_for` function can be used to cancel a task after a timeout.
23
Victor Stinnerea3183f2013-12-03 01:08:00 +010024Locks
25-----
26
Victor Stinner8c462c52014-01-24 18:11:43 +010027Lock
28^^^^
29
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
38 It is created in the unlocked state. It has two basic methods, :meth:`acquire`
39 and :meth:`release`. When the state is unlocked, acquire() changes the state to
40 locked and returns immediately. When the state is locked, acquire() blocks
41 until a call to release() in another coroutine changes it to unlocked, then
42 the acquire() call resets it to locked and returns. The release() method
43 should only be called in the locked state; it changes the state to unlocked
44 and returns immediately. If an attempt is made to release an unlocked lock,
45 a :exc:`RuntimeError` will be raised.
46
47 When more than one coroutine is blocked in acquire() waiting for the state
48 to turn to unlocked, only one coroutine proceeds when a release() call
49 resets the state to unlocked; first coroutine which is blocked in acquire()
50 is being processed.
51
52 :meth:`acquire` is a coroutine and should be called with ``yield from``.
53
Serhiy Storchaka14867992014-09-10 23:43:41 +030054 Locks also support the context management protocol. ``(yield from lock)``
Victor Stinnerea3183f2013-12-03 01:08:00 +010055 should be used as context manager expression.
56
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 Usage::
60
61 lock = Lock()
62 ...
63 yield from lock
64 try:
65 ...
66 finally:
67 lock.release()
68
69 Context manager usage::
70
71 lock = Lock()
72 ...
73 with (yield from lock):
74 ...
75
76 Lock objects can be tested for locking state::
77
78 if not lock.locked():
79 yield from lock
80 else:
81 # lock is acquired
82 ...
83
84 .. method:: locked()
85
Larry Hastings3732ed22014-03-15 21:13:56 -070086 Return ``True`` if the lock is acquired.
Victor Stinnerea3183f2013-12-03 01:08:00 +010087
Victor Stinnerbdd574d2015-02-12 22:49:18 +010088 .. coroutinemethod:: acquire()
Victor Stinnerea3183f2013-12-03 01:08:00 +010089
90 Acquire a lock.
91
92 This method blocks until the lock is unlocked, then sets it to locked and
93 returns ``True``.
94
Larry Hastings3732ed22014-03-15 21:13:56 -070095 This method is a :ref:`coroutine <coroutine>`.
Victor Stinnerea3183f2013-12-03 01:08:00 +010096
97 .. method:: release()
98
99 Release a lock.
100
101 When the lock is locked, reset it to unlocked, and return. If any other
102 coroutines are blocked waiting for the lock to become unlocked, allow
103 exactly one of them to proceed.
104
105 When invoked on an unlocked lock, a :exc:`RuntimeError` is raised.
106
107 There is no return value.
108
109
Victor Stinner8c462c52014-01-24 18:11:43 +0100110Event
111^^^^^
112
Victor Stinnerea3183f2013-12-03 01:08:00 +0100113.. class:: Event(\*, loop=None)
114
115 An Event implementation, asynchronous equivalent to :class:`threading.Event`.
116
117 Class implementing event objects. An event manages a flag that can be set to
118 true with the :meth:`set` method and reset to false with the :meth:`clear`
119 method. The :meth:`wait` method blocks until the flag is true. The flag is
120 initially false.
121
Victor Stinner7b2c3c62015-02-26 10:39:16 +0100122 This class is :ref:`not thread safe <asyncio-multithreading>`.
123
Victor Stinnerea3183f2013-12-03 01:08:00 +0100124 .. method:: clear()
125
126 Reset the internal flag to false. Subsequently, coroutines calling
127 :meth:`wait` will block until :meth:`set` is called to set the internal
128 flag to true again.
129
130 .. method:: is_set()
131
132 Return ``True`` if and only if the internal flag is true.
133
134 .. method:: set()
135
136 Set the internal flag to true. All coroutines waiting for it to become
137 true are awakened. Coroutine that call :meth:`wait` once the flag is true
138 will not block at all.
139
Victor Stinnerbdd574d2015-02-12 22:49:18 +0100140 .. coroutinemethod:: wait()
Victor Stinnerea3183f2013-12-03 01:08:00 +0100141
142 Block until the internal flag is true.
143
144 If the internal flag is true on entry, return ``True`` immediately.
145 Otherwise, block until another coroutine calls :meth:`set` to set the
146 flag to true, then return ``True``.
147
Yury Selivanov37f15bc2014-02-20 16:20:44 -0500148 This method is a :ref:`coroutine <coroutine>`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100149
150
Victor Stinner8c462c52014-01-24 18:11:43 +0100151Condition
152^^^^^^^^^
153
Andrew Svetlovf200ce62014-07-26 19:50:37 +0300154.. class:: Condition(lock=None, \*, loop=None)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100155
156 A Condition implementation, asynchronous equivalent to
157 :class:`threading.Condition`.
158
159 This class implements condition variable objects. A condition variable
160 allows one or more coroutines to wait until they are notified by another
161 coroutine.
162
Andrew Svetlovf200ce62014-07-26 19:50:37 +0300163 If the *lock* argument is given and not ``None``, it must be a :class:`Lock`
164 object, and it is used as the underlying lock. Otherwise,
165 a new :class:`Lock` object is created and used as the underlying lock.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100166
Victor Stinner7b2c3c62015-02-26 10:39:16 +0100167 This class is :ref:`not thread safe <asyncio-multithreading>`.
168
Victor Stinnerbdd574d2015-02-12 22:49:18 +0100169 .. coroutinemethod:: acquire()
Larry Hastings3732ed22014-03-15 21:13:56 -0700170
171 Acquire the underlying lock.
172
173 This method blocks until the lock is unlocked, then sets it to locked and
174 returns ``True``.
175
176 This method is a :ref:`coroutine <coroutine>`.
177
Victor Stinnerea3183f2013-12-03 01:08:00 +0100178 .. method:: notify(n=1)
179
180 By default, wake up one coroutine waiting on this condition, if any.
181 If the calling coroutine has not acquired the lock when this method is
182 called, a :exc:`RuntimeError` is raised.
183
184 This method wakes up at most *n* of the coroutines waiting for the
185 condition variable; it is a no-op if no coroutines are waiting.
186
187 .. note::
188
189 An awakened coroutine does not actually return from its :meth:`wait`
190 call until it can reacquire the lock. Since :meth:`notify` does not
191 release the lock, its caller should.
192
Larry Hastings3732ed22014-03-15 21:13:56 -0700193 .. method:: locked()
194
195 Return ``True`` if the underlying lock is acquired.
196
Victor Stinnerea3183f2013-12-03 01:08:00 +0100197 .. method:: notify_all()
198
Zachary Ware828946e2015-05-06 20:19:06 -0500199 Wake up all coroutines waiting on this condition. This method acts like
200 :meth:`notify`, but wakes up all waiting coroutines instead of one. If the
201 calling coroutine has not acquired the lock when this method is called, a
Victor Stinnerea3183f2013-12-03 01:08:00 +0100202 :exc:`RuntimeError` is raised.
203
Larry Hastings3732ed22014-03-15 21:13:56 -0700204 .. method:: release()
205
206 Release the underlying lock.
207
208 When the lock is locked, reset it to unlocked, and return. If any other
209 coroutines are blocked waiting for the lock to become unlocked, allow
210 exactly one of them to proceed.
211
212 When invoked on an unlocked lock, a :exc:`RuntimeError` is raised.
213
214 There is no return value.
215
Victor Stinnerbdd574d2015-02-12 22:49:18 +0100216 .. coroutinemethod:: wait()
Victor Stinnerea3183f2013-12-03 01:08:00 +0100217
218 Wait until notified.
219
220 If the calling coroutine has not acquired the lock when this method is
221 called, a :exc:`RuntimeError` is raised.
222
223 This method releases the underlying lock, and then blocks until it is
224 awakened by a :meth:`notify` or :meth:`notify_all` call for the same
225 condition variable in another coroutine. Once awakened, it re-acquires
226 the lock and returns ``True``.
227
Yury Selivanov37f15bc2014-02-20 16:20:44 -0500228 This method is a :ref:`coroutine <coroutine>`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100229
Victor Stinnerbdd574d2015-02-12 22:49:18 +0100230 .. coroutinemethod:: wait_for(predicate)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100231
232 Wait until a predicate becomes true.
233
234 The predicate should be a callable which result will be interpreted as a
235 boolean value. The final predicate value is the return value.
236
Yury Selivanov37f15bc2014-02-20 16:20:44 -0500237 This method is a :ref:`coroutine <coroutine>`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100238
239
240Semaphores
241----------
242
Victor Stinner8c462c52014-01-24 18:11:43 +0100243Semaphore
244^^^^^^^^^
245
Victor Stinnerea3183f2013-12-03 01:08:00 +0100246.. class:: Semaphore(value=1, \*, loop=None)
247
248 A Semaphore implementation.
249
250 A semaphore manages an internal counter which is decremented by each
251 :meth:`acquire` call and incremented by each :meth:`release` call. The
252 counter can never go below zero; when :meth:`acquire` finds that it is zero,
Zachary Ware828946e2015-05-06 20:19:06 -0500253 it blocks, waiting until some other coroutine calls :meth:`release`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100254
Serhiy Storchaka14867992014-09-10 23:43:41 +0300255 Semaphores also support the context management protocol.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100256
257 The optional argument gives the initial value for the internal counter; it
258 defaults to ``1``. If the value given is less than ``0``, :exc:`ValueError`
259 is raised.
260
Victor Stinner7b2c3c62015-02-26 10:39:16 +0100261 This class is :ref:`not thread safe <asyncio-multithreading>`.
262
Victor Stinnerbdd574d2015-02-12 22:49:18 +0100263 .. coroutinemethod:: acquire()
Victor Stinnerea3183f2013-12-03 01:08:00 +0100264
265 Acquire a semaphore.
266
267 If the internal counter is larger than zero on entry, decrement it by one
268 and return ``True`` immediately. If it is zero on entry, block, waiting
269 until some other coroutine has called :meth:`release` to make it larger
270 than ``0``, and then return ``True``.
271
Yury Selivanov37f15bc2014-02-20 16:20:44 -0500272 This method is a :ref:`coroutine <coroutine>`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100273
274 .. method:: locked()
275
276 Returns ``True`` if semaphore can not be acquired immediately.
277
Andrew Svetlovb79e0122015-05-08 14:13:41 +0300278 .. method:: release()
Victor Stinnerea3183f2013-12-03 01:08:00 +0100279
280 Release a semaphore, incrementing the internal counter by one. When it
281 was zero on entry and another coroutine is waiting for it to become
282 larger than zero again, wake up that coroutine.
283
284
Victor Stinner8c462c52014-01-24 18:11:43 +0100285BoundedSemaphore
286^^^^^^^^^^^^^^^^
287
Victor Stinnerea3183f2013-12-03 01:08:00 +0100288.. class:: BoundedSemaphore(value=1, \*, loop=None)
289
Victor Stinner7b2c3c62015-02-26 10:39:16 +0100290 A bounded semaphore implementation. Inherit from :class:`Semaphore`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100291
Victor Stinner7b2c3c62015-02-26 10:39:16 +0100292 This raises :exc:`ValueError` in :meth:`~Semaphore.release` if it would
293 increase the value above the initial value.