blob: a299f09085e7aae8887ca73acea8e444cc8cb152 [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
Larry Hastings3732ed22014-03-15 21:13:56 -070067 Return ``True`` if the lock is acquired.
Victor Stinnerea3183f2013-12-03 01:08:00 +010068
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
Larry Hastings3732ed22014-03-15 21:13:56 -070076 This method is a :ref:`coroutine <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
Yury Selivanov37f15bc2014-02-20 16:20:44 -0500127 This method is a :ref:`coroutine <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
Larry Hastings3732ed22014-03-15 21:13:56 -0700144 .. method:: acquire()
145
146 Acquire the underlying lock.
147
148 This method blocks until the lock is unlocked, then sets it to locked and
149 returns ``True``.
150
151 This method is a :ref:`coroutine <coroutine>`.
152
Victor Stinnerea3183f2013-12-03 01:08:00 +0100153 .. method:: notify(n=1)
154
155 By default, wake up one coroutine waiting on this condition, if any.
156 If the calling coroutine has not acquired the lock when this method is
157 called, a :exc:`RuntimeError` is raised.
158
159 This method wakes up at most *n* of the coroutines waiting for the
160 condition variable; it is a no-op if no coroutines are waiting.
161
162 .. note::
163
164 An awakened coroutine does not actually return from its :meth:`wait`
165 call until it can reacquire the lock. Since :meth:`notify` does not
166 release the lock, its caller should.
167
Larry Hastings3732ed22014-03-15 21:13:56 -0700168 .. method:: locked()
169
170 Return ``True`` if the underlying lock is acquired.
171
Victor Stinnerea3183f2013-12-03 01:08:00 +0100172 .. method:: notify_all()
173
174 Wake up all threads waiting on this condition. This method acts like
175 :meth:`notify`, but wakes up all waiting threads instead of one. If the
176 calling thread has not acquired the lock when this method is called, a
177 :exc:`RuntimeError` is raised.
178
Larry Hastings3732ed22014-03-15 21:13:56 -0700179 .. method:: release()
180
181 Release the underlying lock.
182
183 When the lock is locked, reset it to unlocked, and return. If any other
184 coroutines are blocked waiting for the lock to become unlocked, allow
185 exactly one of them to proceed.
186
187 When invoked on an unlocked lock, a :exc:`RuntimeError` is raised.
188
189 There is no return value.
190
Victor Stinnerea3183f2013-12-03 01:08:00 +0100191 .. method:: wait()
192
193 Wait until notified.
194
195 If the calling coroutine has not acquired the lock when this method is
196 called, a :exc:`RuntimeError` is raised.
197
198 This method releases the underlying lock, and then blocks until it is
199 awakened by a :meth:`notify` or :meth:`notify_all` call for the same
200 condition variable in another coroutine. Once awakened, it re-acquires
201 the lock and returns ``True``.
202
Yury Selivanov37f15bc2014-02-20 16:20:44 -0500203 This method is a :ref:`coroutine <coroutine>`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100204
205 .. method:: wait_for(predicate)
206
207 Wait until a predicate becomes true.
208
209 The predicate should be a callable which result will be interpreted as a
210 boolean value. The final predicate value is the return value.
211
Yury Selivanov37f15bc2014-02-20 16:20:44 -0500212 This method is a :ref:`coroutine <coroutine>`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100213
214
215Semaphores
216----------
217
Victor Stinner8c462c52014-01-24 18:11:43 +0100218Semaphore
219^^^^^^^^^
220
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,
228 it blocks, waiting until some other thread calls :meth:`release`.
229
230 Semaphores also support the context manager protocol.
231
232 The optional argument gives the initial value for the internal counter; it
233 defaults to ``1``. If the value given is less than ``0``, :exc:`ValueError`
234 is raised.
235
236 .. method:: acquire()
237
238 Acquire a semaphore.
239
240 If the internal counter is larger than zero on entry, decrement it by one
241 and return ``True`` immediately. If it is zero on entry, block, waiting
242 until some other coroutine has called :meth:`release` to make it larger
243 than ``0``, and then return ``True``.
244
Yury Selivanov37f15bc2014-02-20 16:20:44 -0500245 This method is a :ref:`coroutine <coroutine>`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100246
247 .. method:: locked()
248
249 Returns ``True`` if semaphore can not be acquired immediately.
250
251 .. method:: release()
252
253 Release a semaphore, incrementing the internal counter by one. When it
254 was zero on entry and another coroutine is waiting for it to become
255 larger than zero again, wake up that coroutine.
256
257
Victor Stinner8c462c52014-01-24 18:11:43 +0100258BoundedSemaphore
259^^^^^^^^^^^^^^^^
260
Victor Stinnerea3183f2013-12-03 01:08:00 +0100261.. class:: BoundedSemaphore(value=1, \*, loop=None)
262
263 A bounded semaphore implementation. Inherit from :class:`Semaphore`.
264
265 This raises :exc:`ValueError` in :meth:`~Semaphore.release` if it would
266 increase the value above the initial value.
267
268
269Queues
270------
271
Victor Stinner8c462c52014-01-24 18:11:43 +0100272Queue
273^^^^^
274
Victor Stinnerea3183f2013-12-03 01:08:00 +0100275.. class:: Queue(maxsize=0, \*, loop=None)
276
277 A queue, useful for coordinating producer and consumer coroutines.
278
279 If *maxsize* is less than or equal to zero, the queue size is infinite. If
280 it is an integer greater than ``0``, then ``yield from put()`` will block
281 when the queue reaches *maxsize*, until an item is removed by :meth:`get`.
282
283 Unlike the standard library :mod:`queue`, you can reliably know this Queue's
Victor Stinner2748bc72013-12-13 10:57:04 +0100284 size with :meth:`qsize`, since your single-threaded asyncio application won't
Victor Stinnerea3183f2013-12-03 01:08:00 +0100285 be interrupted between calling :meth:`qsize` and doing an operation on the
286 Queue.
287
288 .. method:: empty()
289
290 Return ``True`` if the queue is empty, ``False`` otherwise.
291
292 .. method:: full()
293
294 Return ``True`` if there are maxsize items in the queue.
295
296 .. note::
297
298 If the Queue was initialized with ``maxsize=0`` (the default), then
299 :meth:`full()` is never ``True``.
300
301 .. method:: get()
302
303 Remove and return an item from the queue.
304
305 If you yield from :meth:`get()`, wait until a item is available.
306
Yury Selivanov37f15bc2014-02-20 16:20:44 -0500307 This method is a :ref:`coroutine <coroutine>`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100308
309 .. method:: get_nowait()
310
311 Remove and return an item from the queue.
312
313 Return an item if one is immediately available, else raise
Guido van Rossum9ad116b2014-01-25 17:38:31 -0800314 :exc:`QueueEmpty`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100315
316 .. method:: put(item)
317
318 Put an item into the queue.
319
320 If you yield from ``put()``, wait until a free slot is available before
321 adding item.
322
Yury Selivanov37f15bc2014-02-20 16:20:44 -0500323 This method is a :ref:`coroutine <coroutine>`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100324
325 .. method:: put_nowait(item)
326
327 Put an item into the queue without blocking.
328
Guido van Rossum9ad116b2014-01-25 17:38:31 -0800329 If no free slot is immediately available, raise :exc:`QueueFull`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100330
331 .. method:: qsize()
332
333 Number of items in the queue.
334
335 .. attribute:: maxsize
336
337 Number of items allowed in the queue.
338
339
Victor Stinner8c462c52014-01-24 18:11:43 +0100340PriorityQueue
341^^^^^^^^^^^^^
342
Victor Stinnerea3183f2013-12-03 01:08:00 +0100343.. class:: PriorityQueue
344
345 A subclass of :class:`Queue`; retrieves entries in priority order (lowest
346 first).
347
348 Entries are typically tuples of the form: (priority number, data).
349
350
Victor Stinner8c462c52014-01-24 18:11:43 +0100351LifoQueue
352^^^^^^^^^
353
Victor Stinnerea3183f2013-12-03 01:08:00 +0100354.. class:: LifoQueue
355
356 A subclass of :class:`Queue` that retrieves most recently added entries
357 first.
358
359
Victor Stinner8c462c52014-01-24 18:11:43 +0100360JoinableQueue
361^^^^^^^^^^^^^
362
Victor Stinnerea3183f2013-12-03 01:08:00 +0100363.. class:: JoinableQueue
364
365 A subclass of :class:`Queue` with :meth:`task_done` and :meth:`join`
366 methods.
367
368 .. method:: join()
369
370 Block until all items in the queue have been gotten and processed.
371
372 The count of unfinished tasks goes up whenever an item is added to the
373 queue. The count goes down whenever a consumer thread calls
374 :meth:`task_done` to indicate that the item was retrieved and all work on
375 it is complete. When the count of unfinished tasks drops to zero,
376 :meth:`join` unblocks.
377
Yury Selivanov37f15bc2014-02-20 16:20:44 -0500378 This method is a :ref:`coroutine <coroutine>`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100379
380 .. method:: task_done()
381
382 Indicate that a formerly enqueued task is complete.
383
384 Used by queue consumers. For each :meth:`~Queue.get` used to fetch a task, a
385 subsequent call to :meth:`task_done` tells the queue that the processing
386 on the task is complete.
387
388 If a :meth:`join` is currently blocking, it will resume when all items
389 have been processed (meaning that a :meth:`task_done` call was received
390 for every item that had been :meth:`~Queue.put` into the queue).
391
392 Raises :exc:`ValueError` if called more times than there were items
393 placed in the queue.
394
Guido van Rossum9ad116b2014-01-25 17:38:31 -0800395
396Exceptions
397^^^^^^^^^^
398
399.. exception:: QueueEmpty
400
401 Exception raised when non-blocking :meth:`~Queue.get` (or
402 :meth:`~Queue.get_nowait`) is called
403 on a :class:`Queue` object which is empty.
404
405
406.. exception:: QueueFull
407
408 Exception raised when non-blocking :meth:`~Queue.put` (or
409 :meth:`~Queue.put_nowait`) is called
410 on a :class:`Queue` object which is full.