blob: c63447bbab41fc2d655c5add9807308b186aac26 [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
Serhiy Storchaka14867992014-09-10 23:43:41 +030037 Locks also support the context management protocol. ``(yield from lock)``
Victor Stinnerea3183f2013-12-03 01:08:00 +010038 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
Andrew Svetlovf200ce62014-07-26 19:50:37 +0300133.. class:: Condition(lock=None, \*, loop=None)
Victor Stinnerea3183f2013-12-03 01:08:00 +0100134
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
Andrew Svetlovf200ce62014-07-26 19:50:37 +0300142 If the *lock* argument is given and not ``None``, it must be a :class:`Lock`
143 object, and it is used as the underlying lock. Otherwise,
144 a new :class:`Lock` object is created and used as the underlying lock.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100145
Larry Hastings3732ed22014-03-15 21:13:56 -0700146 .. method:: acquire()
147
148 Acquire the underlying lock.
149
150 This method blocks until the lock is unlocked, then sets it to locked and
151 returns ``True``.
152
153 This method is a :ref:`coroutine <coroutine>`.
154
Victor Stinnerea3183f2013-12-03 01:08:00 +0100155 .. method:: notify(n=1)
156
157 By default, wake up one coroutine waiting on this condition, if any.
158 If the calling coroutine has not acquired the lock when this method is
159 called, a :exc:`RuntimeError` is raised.
160
161 This method wakes up at most *n* of the coroutines waiting for the
162 condition variable; it is a no-op if no coroutines are waiting.
163
164 .. note::
165
166 An awakened coroutine does not actually return from its :meth:`wait`
167 call until it can reacquire the lock. Since :meth:`notify` does not
168 release the lock, its caller should.
169
Larry Hastings3732ed22014-03-15 21:13:56 -0700170 .. method:: locked()
171
172 Return ``True`` if the underlying lock is acquired.
173
Victor Stinnerea3183f2013-12-03 01:08:00 +0100174 .. method:: notify_all()
175
176 Wake up all threads waiting on this condition. This method acts like
177 :meth:`notify`, but wakes up all waiting threads instead of one. If the
178 calling thread has not acquired the lock when this method is called, a
179 :exc:`RuntimeError` is raised.
180
Larry Hastings3732ed22014-03-15 21:13:56 -0700181 .. method:: release()
182
183 Release the underlying lock.
184
185 When the lock is locked, reset it to unlocked, and return. If any other
186 coroutines are blocked waiting for the lock to become unlocked, allow
187 exactly one of them to proceed.
188
189 When invoked on an unlocked lock, a :exc:`RuntimeError` is raised.
190
191 There is no return value.
192
Victor Stinnerea3183f2013-12-03 01:08:00 +0100193 .. method:: wait()
194
195 Wait until notified.
196
197 If the calling coroutine has not acquired the lock when this method is
198 called, a :exc:`RuntimeError` is raised.
199
200 This method releases the underlying lock, and then blocks until it is
201 awakened by a :meth:`notify` or :meth:`notify_all` call for the same
202 condition variable in another coroutine. Once awakened, it re-acquires
203 the lock and returns ``True``.
204
Yury Selivanov37f15bc2014-02-20 16:20:44 -0500205 This method is a :ref:`coroutine <coroutine>`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100206
207 .. method:: wait_for(predicate)
208
209 Wait until a predicate becomes true.
210
211 The predicate should be a callable which result will be interpreted as a
212 boolean value. The final predicate value is the return value.
213
Yury Selivanov37f15bc2014-02-20 16:20:44 -0500214 This method is a :ref:`coroutine <coroutine>`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100215
216
217Semaphores
218----------
219
Victor Stinner8c462c52014-01-24 18:11:43 +0100220Semaphore
221^^^^^^^^^
222
Victor Stinnerea3183f2013-12-03 01:08:00 +0100223.. class:: Semaphore(value=1, \*, loop=None)
224
225 A Semaphore implementation.
226
227 A semaphore manages an internal counter which is decremented by each
228 :meth:`acquire` call and incremented by each :meth:`release` call. The
229 counter can never go below zero; when :meth:`acquire` finds that it is zero,
230 it blocks, waiting until some other thread calls :meth:`release`.
231
Serhiy Storchaka14867992014-09-10 23:43:41 +0300232 Semaphores also support the context management protocol.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100233
234 The optional argument gives the initial value for the internal counter; it
235 defaults to ``1``. If the value given is less than ``0``, :exc:`ValueError`
236 is raised.
237
238 .. method:: acquire()
239
240 Acquire a semaphore.
241
242 If the internal counter is larger than zero on entry, decrement it by one
243 and return ``True`` immediately. If it is zero on entry, block, waiting
244 until some other coroutine has called :meth:`release` to make it larger
245 than ``0``, and then return ``True``.
246
Yury Selivanov37f15bc2014-02-20 16:20:44 -0500247 This method is a :ref:`coroutine <coroutine>`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100248
249 .. method:: locked()
250
251 Returns ``True`` if semaphore can not be acquired immediately.
252
253 .. method:: release()
254
255 Release a semaphore, incrementing the internal counter by one. When it
256 was zero on entry and another coroutine is waiting for it to become
257 larger than zero again, wake up that coroutine.
258
259
Victor Stinner8c462c52014-01-24 18:11:43 +0100260BoundedSemaphore
261^^^^^^^^^^^^^^^^
262
Victor Stinnerea3183f2013-12-03 01:08:00 +0100263.. class:: BoundedSemaphore(value=1, \*, loop=None)
264
265 A bounded semaphore implementation. Inherit from :class:`Semaphore`.
266
267 This raises :exc:`ValueError` in :meth:`~Semaphore.release` if it would
268 increase the value above the initial value.
269
270
271Queues
272------
273
Victor Stinner8c462c52014-01-24 18:11:43 +0100274Queue
275^^^^^
276
Victor Stinnerea3183f2013-12-03 01:08:00 +0100277.. class:: Queue(maxsize=0, \*, loop=None)
278
279 A queue, useful for coordinating producer and consumer coroutines.
280
281 If *maxsize* is less than or equal to zero, the queue size is infinite. If
282 it is an integer greater than ``0``, then ``yield from put()`` will block
283 when the queue reaches *maxsize*, until an item is removed by :meth:`get`.
284
285 Unlike the standard library :mod:`queue`, you can reliably know this Queue's
Victor Stinner2748bc72013-12-13 10:57:04 +0100286 size with :meth:`qsize`, since your single-threaded asyncio application won't
Victor Stinnerea3183f2013-12-03 01:08:00 +0100287 be interrupted between calling :meth:`qsize` and doing an operation on the
288 Queue.
289
290 .. method:: empty()
291
292 Return ``True`` if the queue is empty, ``False`` otherwise.
293
294 .. method:: full()
295
Victor Stinner4f9b7732014-12-22 22:07:06 +0100296 Return ``True`` if there are :attr:`maxsize` items in the queue.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100297
298 .. note::
299
300 If the Queue was initialized with ``maxsize=0`` (the default), then
301 :meth:`full()` is never ``True``.
302
303 .. method:: get()
304
Victor Stinner4f9b7732014-12-22 22:07:06 +0100305 Remove and return an item from the queue. If queue is empty, wait until
306 an item is available.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100307
Yury Selivanov37f15bc2014-02-20 16:20:44 -0500308 This method is a :ref:`coroutine <coroutine>`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100309
Victor Stinner4f9b7732014-12-22 22:07:06 +0100310 .. seealso::
311
312 The :meth:`empty` method.
313
Victor Stinnerea3183f2013-12-03 01:08:00 +0100314 .. method:: get_nowait()
315
316 Remove and return an item from the queue.
317
318 Return an item if one is immediately available, else raise
Guido van Rossum9ad116b2014-01-25 17:38:31 -0800319 :exc:`QueueEmpty`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100320
321 .. method:: put(item)
322
Victor Stinner4f9b7732014-12-22 22:07:06 +0100323 Put an item into the queue. If the queue is full, wait until a free slot
324 is available before adding item.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100325
Yury Selivanov37f15bc2014-02-20 16:20:44 -0500326 This method is a :ref:`coroutine <coroutine>`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100327
Victor Stinner4f9b7732014-12-22 22:07:06 +0100328 .. seealso::
329
330 The :meth:`full` method.
331
Victor Stinnerea3183f2013-12-03 01:08:00 +0100332 .. method:: put_nowait(item)
333
334 Put an item into the queue without blocking.
335
Guido van Rossum9ad116b2014-01-25 17:38:31 -0800336 If no free slot is immediately available, raise :exc:`QueueFull`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100337
338 .. method:: qsize()
339
340 Number of items in the queue.
341
342 .. attribute:: maxsize
343
344 Number of items allowed in the queue.
345
346
Victor Stinner8c462c52014-01-24 18:11:43 +0100347PriorityQueue
348^^^^^^^^^^^^^
349
Victor Stinnerea3183f2013-12-03 01:08:00 +0100350.. class:: PriorityQueue
351
352 A subclass of :class:`Queue`; retrieves entries in priority order (lowest
353 first).
354
355 Entries are typically tuples of the form: (priority number, data).
356
357
Victor Stinner8c462c52014-01-24 18:11:43 +0100358LifoQueue
359^^^^^^^^^
360
Victor Stinnerea3183f2013-12-03 01:08:00 +0100361.. class:: LifoQueue
362
363 A subclass of :class:`Queue` that retrieves most recently added entries
364 first.
365
366
Victor Stinner8c462c52014-01-24 18:11:43 +0100367JoinableQueue
368^^^^^^^^^^^^^
369
Victor Stinnerea3183f2013-12-03 01:08:00 +0100370.. class:: JoinableQueue
371
372 A subclass of :class:`Queue` with :meth:`task_done` and :meth:`join`
373 methods.
374
375 .. method:: join()
376
377 Block until all items in the queue have been gotten and processed.
378
379 The count of unfinished tasks goes up whenever an item is added to the
380 queue. The count goes down whenever a consumer thread calls
381 :meth:`task_done` to indicate that the item was retrieved and all work on
382 it is complete. When the count of unfinished tasks drops to zero,
383 :meth:`join` unblocks.
384
Yury Selivanov37f15bc2014-02-20 16:20:44 -0500385 This method is a :ref:`coroutine <coroutine>`.
Victor Stinnerea3183f2013-12-03 01:08:00 +0100386
387 .. method:: task_done()
388
389 Indicate that a formerly enqueued task is complete.
390
391 Used by queue consumers. For each :meth:`~Queue.get` used to fetch a task, a
392 subsequent call to :meth:`task_done` tells the queue that the processing
393 on the task is complete.
394
395 If a :meth:`join` is currently blocking, it will resume when all items
396 have been processed (meaning that a :meth:`task_done` call was received
397 for every item that had been :meth:`~Queue.put` into the queue).
398
399 Raises :exc:`ValueError` if called more times than there were items
400 placed in the queue.
401
Guido van Rossum9ad116b2014-01-25 17:38:31 -0800402
403Exceptions
404^^^^^^^^^^
405
406.. exception:: QueueEmpty
407
408 Exception raised when non-blocking :meth:`~Queue.get` (or
409 :meth:`~Queue.get_nowait`) is called
410 on a :class:`Queue` object which is empty.
411
412
413.. exception:: QueueFull
414
415 Exception raised when non-blocking :meth:`~Queue.put` (or
416 :meth:`~Queue.put_nowait`) is called
417 on a :class:`Queue` object which is full.