blob: 96ea012f7123e4783b9cdc78be409040779ef32a [file] [log] [blame]
R David Murray6a143812013-12-20 14:37:39 -05001.. currentmodule:: asyncio
Victor Stinnerea3183f2013-12-03 01:08:00 +01002.. _sync:
3
4Synchronization primitives
5==========================
6
7Locks
8-----
9
10.. class:: Lock(\*, loop=None)
11
12 Primitive lock objects.
13
14 A primitive lock is a synchronization primitive that is not owned by a
15 particular coroutine when locked. A primitive lock is in one of two states,
16 'locked' or 'unlocked'.
17
18 It is created in the unlocked state. It has two basic methods, :meth:`acquire`
19 and :meth:`release`. When the state is unlocked, acquire() changes the state to
20 locked and returns immediately. When the state is locked, acquire() blocks
21 until a call to release() in another coroutine changes it to unlocked, then
22 the acquire() call resets it to locked and returns. The release() method
23 should only be called in the locked state; it changes the state to unlocked
24 and returns immediately. If an attempt is made to release an unlocked lock,
25 a :exc:`RuntimeError` will be raised.
26
27 When more than one coroutine is blocked in acquire() waiting for the state
28 to turn to unlocked, only one coroutine proceeds when a release() call
29 resets the state to unlocked; first coroutine which is blocked in acquire()
30 is being processed.
31
32 :meth:`acquire` is a coroutine and should be called with ``yield from``.
33
34 Locks also support the context manager protocol. ``(yield from lock)``
35 should be used as context manager expression.
36
37 Usage::
38
39 lock = Lock()
40 ...
41 yield from lock
42 try:
43 ...
44 finally:
45 lock.release()
46
47 Context manager usage::
48
49 lock = Lock()
50 ...
51 with (yield from lock):
52 ...
53
54 Lock objects can be tested for locking state::
55
56 if not lock.locked():
57 yield from lock
58 else:
59 # lock is acquired
60 ...
61
62 .. method:: locked()
63
64 Return ``True`` if lock is acquired.
65
66 .. method:: acquire()
67
68 Acquire a lock.
69
70 This method blocks until the lock is unlocked, then sets it to locked and
71 returns ``True``.
72
Victor Stinner59759ff2014-01-16 19:30:21 +010073 This method returns a :ref:`coroutine object <coroutine>`.
Victor Stinnerea3183f2013-12-03 01:08:00 +010074
75 .. method:: release()
76
77 Release a lock.
78
79 When the lock is locked, reset it to unlocked, and return. If any other
80 coroutines are blocked waiting for the lock to become unlocked, allow
81 exactly one of them to proceed.
82
83 When invoked on an unlocked lock, a :exc:`RuntimeError` is raised.
84
85 There is no return value.
86
87
88.. 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
97 .. method:: clear()
98
99 Reset the internal flag to false. Subsequently, coroutines calling
100 :meth:`wait` will block until :meth:`set` is called to set the internal
101 flag to true again.
102
103 .. method:: is_set()
104
105 Return ``True`` if and only if the internal flag is true.
106
107 .. method:: set()
108
109 Set the internal flag to true. All coroutines waiting for it to become
110 true are awakened. Coroutine that call :meth:`wait` once the flag is true
111 will not block at all.
112
113 .. method:: wait()
114
115 Block until the internal flag is true.
116
117 If the internal flag is true on entry, return ``True`` immediately.
118 Otherwise, block until another coroutine calls :meth:`set` to set the
119 flag to true, then return ``True``.
120
Victor Stinner59759ff2014-01-16 19:30:21 +0100121 This method returns a :ref:`coroutine object <coroutine>`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100122
123
124.. class:: Condition(\*, loop=None)
125
126 A Condition implementation, asynchronous equivalent to
127 :class:`threading.Condition`.
128
129 This class implements condition variable objects. A condition variable
130 allows one or more coroutines to wait until they are notified by another
131 coroutine.
132
133 A new :class:`Lock` object is created and used as the underlying lock.
134
135 .. method:: notify(n=1)
136
137 By default, wake up one coroutine waiting on this condition, if any.
138 If the calling coroutine has not acquired the lock when this method is
139 called, a :exc:`RuntimeError` is raised.
140
141 This method wakes up at most *n* of the coroutines waiting for the
142 condition variable; it is a no-op if no coroutines are waiting.
143
144 .. note::
145
146 An awakened coroutine does not actually return from its :meth:`wait`
147 call until it can reacquire the lock. Since :meth:`notify` does not
148 release the lock, its caller should.
149
150 .. method:: notify_all()
151
152 Wake up all threads waiting on this condition. This method acts like
153 :meth:`notify`, but wakes up all waiting threads instead of one. If the
154 calling thread has not acquired the lock when this method is called, a
155 :exc:`RuntimeError` is raised.
156
157 .. method:: wait()
158
159 Wait until notified.
160
161 If the calling coroutine has not acquired the lock when this method is
162 called, a :exc:`RuntimeError` is raised.
163
164 This method releases the underlying lock, and then blocks until it is
165 awakened by a :meth:`notify` or :meth:`notify_all` call for the same
166 condition variable in another coroutine. Once awakened, it re-acquires
167 the lock and returns ``True``.
168
Victor Stinner59759ff2014-01-16 19:30:21 +0100169 This method returns a :ref:`coroutine object <coroutine>`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100170
171 .. method:: wait_for(predicate)
172
173 Wait until a predicate becomes true.
174
175 The predicate should be a callable which result will be interpreted as a
176 boolean value. The final predicate value is the return value.
177
Victor Stinner59759ff2014-01-16 19:30:21 +0100178 This method returns a :ref:`coroutine object <coroutine>`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100179
180
181Semaphores
182----------
183
184.. class:: Semaphore(value=1, \*, loop=None)
185
186 A Semaphore implementation.
187
188 A semaphore manages an internal counter which is decremented by each
189 :meth:`acquire` call and incremented by each :meth:`release` call. The
190 counter can never go below zero; when :meth:`acquire` finds that it is zero,
191 it blocks, waiting until some other thread calls :meth:`release`.
192
193 Semaphores also support the context manager protocol.
194
195 The optional argument gives the initial value for the internal counter; it
196 defaults to ``1``. If the value given is less than ``0``, :exc:`ValueError`
197 is raised.
198
199 .. method:: acquire()
200
201 Acquire a semaphore.
202
203 If the internal counter is larger than zero on entry, decrement it by one
204 and return ``True`` immediately. If it is zero on entry, block, waiting
205 until some other coroutine has called :meth:`release` to make it larger
206 than ``0``, and then return ``True``.
207
Victor Stinner59759ff2014-01-16 19:30:21 +0100208 This method returns a :ref:`coroutine object <coroutine>`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100209
210 .. method:: locked()
211
212 Returns ``True`` if semaphore can not be acquired immediately.
213
214 .. method:: release()
215
216 Release a semaphore, incrementing the internal counter by one. When it
217 was zero on entry and another coroutine is waiting for it to become
218 larger than zero again, wake up that coroutine.
219
220
221.. class:: BoundedSemaphore(value=1, \*, loop=None)
222
223 A bounded semaphore implementation. Inherit from :class:`Semaphore`.
224
225 This raises :exc:`ValueError` in :meth:`~Semaphore.release` if it would
226 increase the value above the initial value.
227
228
229Queues
230------
231
232.. class:: Queue(maxsize=0, \*, loop=None)
233
234 A queue, useful for coordinating producer and consumer coroutines.
235
236 If *maxsize* is less than or equal to zero, the queue size is infinite. If
237 it is an integer greater than ``0``, then ``yield from put()`` will block
238 when the queue reaches *maxsize*, until an item is removed by :meth:`get`.
239
240 Unlike the standard library :mod:`queue`, you can reliably know this Queue's
Victor Stinner2748bc72013-12-13 10:57:04 +0100241 size with :meth:`qsize`, since your single-threaded asyncio application won't
Victor Stinnerea3183f2013-12-03 01:08:00 +0100242 be interrupted between calling :meth:`qsize` and doing an operation on the
243 Queue.
244
245 .. method:: empty()
246
247 Return ``True`` if the queue is empty, ``False`` otherwise.
248
249 .. method:: full()
250
251 Return ``True`` if there are maxsize items in the queue.
252
253 .. note::
254
255 If the Queue was initialized with ``maxsize=0`` (the default), then
256 :meth:`full()` is never ``True``.
257
258 .. method:: get()
259
260 Remove and return an item from the queue.
261
262 If you yield from :meth:`get()`, wait until a item is available.
263
Victor Stinner59759ff2014-01-16 19:30:21 +0100264 This method returns a :ref:`coroutine object <coroutine>`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100265
266 .. method:: get_nowait()
267
268 Remove and return an item from the queue.
269
270 Return an item if one is immediately available, else raise
271 :exc:`~queue.Empty`.
272
273 .. method:: put(item)
274
275 Put an item into the queue.
276
277 If you yield from ``put()``, wait until a free slot is available before
278 adding item.
279
Victor Stinner59759ff2014-01-16 19:30:21 +0100280 This method returns a :ref:`coroutine object <coroutine>`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100281
282 .. method:: put_nowait(item)
283
284 Put an item into the queue without blocking.
285
286 If no free slot is immediately available, raise :exc:`~queue.Full`.
287
288 .. method:: qsize()
289
290 Number of items in the queue.
291
292 .. attribute:: maxsize
293
294 Number of items allowed in the queue.
295
296
297.. class:: PriorityQueue
298
299 A subclass of :class:`Queue`; retrieves entries in priority order (lowest
300 first).
301
302 Entries are typically tuples of the form: (priority number, data).
303
304
305.. class:: LifoQueue
306
307 A subclass of :class:`Queue` that retrieves most recently added entries
308 first.
309
310
311.. class:: JoinableQueue
312
313 A subclass of :class:`Queue` with :meth:`task_done` and :meth:`join`
314 methods.
315
316 .. method:: join()
317
318 Block until all items in the queue have been gotten and processed.
319
320 The count of unfinished tasks goes up whenever an item is added to the
321 queue. The count goes down whenever a consumer thread calls
322 :meth:`task_done` to indicate that the item was retrieved and all work on
323 it is complete. When the count of unfinished tasks drops to zero,
324 :meth:`join` unblocks.
325
Victor Stinner59759ff2014-01-16 19:30:21 +0100326 This method returns a :ref:`coroutine object <coroutine>`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100327
328 .. method:: task_done()
329
330 Indicate that a formerly enqueued task is complete.
331
332 Used by queue consumers. For each :meth:`~Queue.get` used to fetch a task, a
333 subsequent call to :meth:`task_done` tells the queue that the processing
334 on the task is complete.
335
336 If a :meth:`join` is currently blocking, it will resume when all items
337 have been processed (meaning that a :meth:`task_done` call was received
338 for every item that had been :meth:`~Queue.put` into the queue).
339
340 Raises :exc:`ValueError` if called more times than there were items
341 placed in the queue.
342