blob: 19e49ffb82021fbf07b77f3b15e8818177e303fc [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
7Locks
8-----
9
Victor Stinner8c462c52014-01-24 18:11:43 +010010Lock
11^^^^
12
Victor Stinnerea3183f2013-12-03 01:08:00 +010013.. class:: Lock(\*, loop=None)
14
15 Primitive lock objects.
16
17 A primitive lock is a synchronization primitive that is not owned by a
18 particular coroutine when locked. A primitive lock is in one of two states,
19 'locked' or 'unlocked'.
20
21 It is created in the unlocked state. It has two basic methods, :meth:`acquire`
22 and :meth:`release`. When the state is unlocked, acquire() changes the state to
23 locked and returns immediately. When the state is locked, acquire() blocks
24 until a call to release() in another coroutine changes it to unlocked, then
25 the acquire() call resets it to locked and returns. The release() method
26 should only be called in the locked state; it changes the state to unlocked
27 and returns immediately. If an attempt is made to release an unlocked lock,
28 a :exc:`RuntimeError` will be raised.
29
30 When more than one coroutine is blocked in acquire() waiting for the state
31 to turn to unlocked, only one coroutine proceeds when a release() call
32 resets the state to unlocked; first coroutine which is blocked in acquire()
33 is being processed.
34
35 :meth:`acquire` is a coroutine and should be called with ``yield from``.
36
37 Locks also support the context manager protocol. ``(yield from lock)``
38 should be used as context manager expression.
39
40 Usage::
41
42 lock = Lock()
43 ...
44 yield from lock
45 try:
46 ...
47 finally:
48 lock.release()
49
50 Context manager usage::
51
52 lock = Lock()
53 ...
54 with (yield from lock):
55 ...
56
57 Lock objects can be tested for locking state::
58
59 if not lock.locked():
60 yield from lock
61 else:
62 # lock is acquired
63 ...
64
65 .. method:: locked()
66
67 Return ``True`` if lock is acquired.
68
69 .. method:: acquire()
70
71 Acquire a lock.
72
73 This method blocks until the lock is unlocked, then sets it to locked and
74 returns ``True``.
75
Victor Stinner59759ff2014-01-16 19:30:21 +010076 This method returns a :ref:`coroutine object <coroutine>`.
Victor Stinnerea3183f2013-12-03 01:08:00 +010077
78 .. method:: release()
79
80 Release a lock.
81
82 When the lock is locked, reset it to unlocked, and return. If any other
83 coroutines are blocked waiting for the lock to become unlocked, allow
84 exactly one of them to proceed.
85
86 When invoked on an unlocked lock, a :exc:`RuntimeError` is raised.
87
88 There is no return value.
89
90
Victor Stinner8c462c52014-01-24 18:11:43 +010091Event
92^^^^^
93
Victor Stinnerea3183f2013-12-03 01:08:00 +010094.. class:: Event(\*, loop=None)
95
96 An Event implementation, asynchronous equivalent to :class:`threading.Event`.
97
98 Class implementing event objects. An event manages a flag that can be set to
99 true with the :meth:`set` method and reset to false with the :meth:`clear`
100 method. The :meth:`wait` method blocks until the flag is true. The flag is
101 initially false.
102
103 .. method:: clear()
104
105 Reset the internal flag to false. Subsequently, coroutines calling
106 :meth:`wait` will block until :meth:`set` is called to set the internal
107 flag to true again.
108
109 .. method:: is_set()
110
111 Return ``True`` if and only if the internal flag is true.
112
113 .. method:: set()
114
115 Set the internal flag to true. All coroutines waiting for it to become
116 true are awakened. Coroutine that call :meth:`wait` once the flag is true
117 will not block at all.
118
119 .. method:: wait()
120
121 Block until the internal flag is true.
122
123 If the internal flag is true on entry, return ``True`` immediately.
124 Otherwise, block until another coroutine calls :meth:`set` to set the
125 flag to true, then return ``True``.
126
Victor Stinner59759ff2014-01-16 19:30:21 +0100127 This method returns a :ref:`coroutine object <coroutine>`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100128
129
Victor Stinner8c462c52014-01-24 18:11:43 +0100130Condition
131^^^^^^^^^
132
Victor Stinnerea3183f2013-12-03 01:08:00 +0100133.. class:: Condition(\*, loop=None)
134
135 A Condition implementation, asynchronous equivalent to
136 :class:`threading.Condition`.
137
138 This class implements condition variable objects. A condition variable
139 allows one or more coroutines to wait until they are notified by another
140 coroutine.
141
142 A new :class:`Lock` object is created and used as the underlying lock.
143
144 .. method:: notify(n=1)
145
146 By default, wake up one coroutine waiting on this condition, if any.
147 If the calling coroutine has not acquired the lock when this method is
148 called, a :exc:`RuntimeError` is raised.
149
150 This method wakes up at most *n* of the coroutines waiting for the
151 condition variable; it is a no-op if no coroutines are waiting.
152
153 .. note::
154
155 An awakened coroutine does not actually return from its :meth:`wait`
156 call until it can reacquire the lock. Since :meth:`notify` does not
157 release the lock, its caller should.
158
159 .. method:: notify_all()
160
161 Wake up all threads waiting on this condition. This method acts like
162 :meth:`notify`, but wakes up all waiting threads instead of one. If the
163 calling thread has not acquired the lock when this method is called, a
164 :exc:`RuntimeError` is raised.
165
166 .. method:: wait()
167
168 Wait until notified.
169
170 If the calling coroutine has not acquired the lock when this method is
171 called, a :exc:`RuntimeError` is raised.
172
173 This method releases the underlying lock, and then blocks until it is
174 awakened by a :meth:`notify` or :meth:`notify_all` call for the same
175 condition variable in another coroutine. Once awakened, it re-acquires
176 the lock and returns ``True``.
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 .. method:: wait_for(predicate)
181
182 Wait until a predicate becomes true.
183
184 The predicate should be a callable which result will be interpreted as a
185 boolean value. The final predicate value is the return value.
186
Victor Stinner59759ff2014-01-16 19:30:21 +0100187 This method returns a :ref:`coroutine object <coroutine>`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100188
189
190Semaphores
191----------
192
Victor Stinner8c462c52014-01-24 18:11:43 +0100193Semaphore
194^^^^^^^^^
195
Victor Stinnerea3183f2013-12-03 01:08:00 +0100196.. class:: Semaphore(value=1, \*, loop=None)
197
198 A Semaphore implementation.
199
200 A semaphore manages an internal counter which is decremented by each
201 :meth:`acquire` call and incremented by each :meth:`release` call. The
202 counter can never go below zero; when :meth:`acquire` finds that it is zero,
203 it blocks, waiting until some other thread calls :meth:`release`.
204
205 Semaphores also support the context manager protocol.
206
207 The optional argument gives the initial value for the internal counter; it
208 defaults to ``1``. If the value given is less than ``0``, :exc:`ValueError`
209 is raised.
210
211 .. method:: acquire()
212
213 Acquire a semaphore.
214
215 If the internal counter is larger than zero on entry, decrement it by one
216 and return ``True`` immediately. If it is zero on entry, block, waiting
217 until some other coroutine has called :meth:`release` to make it larger
218 than ``0``, and then return ``True``.
219
Victor Stinner59759ff2014-01-16 19:30:21 +0100220 This method returns a :ref:`coroutine object <coroutine>`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100221
222 .. method:: locked()
223
224 Returns ``True`` if semaphore can not be acquired immediately.
225
226 .. method:: release()
227
228 Release a semaphore, incrementing the internal counter by one. When it
229 was zero on entry and another coroutine is waiting for it to become
230 larger than zero again, wake up that coroutine.
231
232
Victor Stinner8c462c52014-01-24 18:11:43 +0100233BoundedSemaphore
234^^^^^^^^^^^^^^^^
235
Victor Stinnerea3183f2013-12-03 01:08:00 +0100236.. class:: BoundedSemaphore(value=1, \*, loop=None)
237
238 A bounded semaphore implementation. Inherit from :class:`Semaphore`.
239
240 This raises :exc:`ValueError` in :meth:`~Semaphore.release` if it would
241 increase the value above the initial value.
242
243
244Queues
245------
246
Victor Stinner8c462c52014-01-24 18:11:43 +0100247Queue
248^^^^^
249
Victor Stinnerea3183f2013-12-03 01:08:00 +0100250.. class:: Queue(maxsize=0, \*, loop=None)
251
252 A queue, useful for coordinating producer and consumer coroutines.
253
254 If *maxsize* is less than or equal to zero, the queue size is infinite. If
255 it is an integer greater than ``0``, then ``yield from put()`` will block
256 when the queue reaches *maxsize*, until an item is removed by :meth:`get`.
257
258 Unlike the standard library :mod:`queue`, you can reliably know this Queue's
Victor Stinner2748bc72013-12-13 10:57:04 +0100259 size with :meth:`qsize`, since your single-threaded asyncio application won't
Victor Stinnerea3183f2013-12-03 01:08:00 +0100260 be interrupted between calling :meth:`qsize` and doing an operation on the
261 Queue.
262
263 .. method:: empty()
264
265 Return ``True`` if the queue is empty, ``False`` otherwise.
266
267 .. method:: full()
268
269 Return ``True`` if there are maxsize items in the queue.
270
271 .. note::
272
273 If the Queue was initialized with ``maxsize=0`` (the default), then
274 :meth:`full()` is never ``True``.
275
276 .. method:: get()
277
278 Remove and return an item from the queue.
279
280 If you yield from :meth:`get()`, wait until a item is available.
281
Victor Stinner59759ff2014-01-16 19:30:21 +0100282 This method returns a :ref:`coroutine object <coroutine>`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100283
284 .. method:: get_nowait()
285
286 Remove and return an item from the queue.
287
288 Return an item if one is immediately available, else raise
Guido van Rossum9ad116b2014-01-25 17:38:31 -0800289 :exc:`QueueEmpty`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100290
291 .. method:: put(item)
292
293 Put an item into the queue.
294
295 If you yield from ``put()``, wait until a free slot is available before
296 adding item.
297
Victor Stinner59759ff2014-01-16 19:30:21 +0100298 This method returns a :ref:`coroutine object <coroutine>`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100299
300 .. method:: put_nowait(item)
301
302 Put an item into the queue without blocking.
303
Guido van Rossum9ad116b2014-01-25 17:38:31 -0800304 If no free slot is immediately available, raise :exc:`QueueFull`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100305
306 .. method:: qsize()
307
308 Number of items in the queue.
309
310 .. attribute:: maxsize
311
312 Number of items allowed in the queue.
313
314
Victor Stinner8c462c52014-01-24 18:11:43 +0100315PriorityQueue
316^^^^^^^^^^^^^
317
Victor Stinnerea3183f2013-12-03 01:08:00 +0100318.. class:: PriorityQueue
319
320 A subclass of :class:`Queue`; retrieves entries in priority order (lowest
321 first).
322
323 Entries are typically tuples of the form: (priority number, data).
324
325
Victor Stinner8c462c52014-01-24 18:11:43 +0100326LifoQueue
327^^^^^^^^^
328
Victor Stinnerea3183f2013-12-03 01:08:00 +0100329.. class:: LifoQueue
330
331 A subclass of :class:`Queue` that retrieves most recently added entries
332 first.
333
334
Victor Stinner8c462c52014-01-24 18:11:43 +0100335JoinableQueue
336^^^^^^^^^^^^^
337
Victor Stinnerea3183f2013-12-03 01:08:00 +0100338.. class:: JoinableQueue
339
340 A subclass of :class:`Queue` with :meth:`task_done` and :meth:`join`
341 methods.
342
343 .. method:: join()
344
345 Block until all items in the queue have been gotten and processed.
346
347 The count of unfinished tasks goes up whenever an item is added to the
348 queue. The count goes down whenever a consumer thread calls
349 :meth:`task_done` to indicate that the item was retrieved and all work on
350 it is complete. When the count of unfinished tasks drops to zero,
351 :meth:`join` unblocks.
352
Victor Stinner59759ff2014-01-16 19:30:21 +0100353 This method returns a :ref:`coroutine object <coroutine>`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100354
355 .. method:: task_done()
356
357 Indicate that a formerly enqueued task is complete.
358
359 Used by queue consumers. For each :meth:`~Queue.get` used to fetch a task, a
360 subsequent call to :meth:`task_done` tells the queue that the processing
361 on the task is complete.
362
363 If a :meth:`join` is currently blocking, it will resume when all items
364 have been processed (meaning that a :meth:`task_done` call was received
365 for every item that had been :meth:`~Queue.put` into the queue).
366
367 Raises :exc:`ValueError` if called more times than there were items
368 placed in the queue.
369
Guido van Rossum9ad116b2014-01-25 17:38:31 -0800370
371Exceptions
372^^^^^^^^^^
373
374.. exception:: QueueEmpty
375
376 Exception raised when non-blocking :meth:`~Queue.get` (or
377 :meth:`~Queue.get_nowait`) is called
378 on a :class:`Queue` object which is empty.
379
380
381.. exception:: QueueFull
382
383 Exception raised when non-blocking :meth:`~Queue.put` (or
384 :meth:`~Queue.put_nowait`) is called
385 on a :class:`Queue` object which is full.