Skip to content

Commit 615a58e

Browse files
committed
asyncio doc: move queues to a new page
1 parent 7e91af3 commit 615a58e

File tree

3 files changed

+174
-166
lines changed

3 files changed

+174
-166
lines changed

Doc/library/asyncio-queue.rst

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
.. currentmodule:: asyncio
2+
3+
Queues
4+
======
5+
6+
Queues:
7+
8+
* :class:`Queue`
9+
* :class:`PriorityQueue`
10+
* :class:`LifoQueue`
11+
* :class:`JoinableQueue`
12+
13+
asyncio queue API was designed to be close to classes of the :mod:`queue`
14+
module (:class:`~queue.Queue`, :class:`~queue.PriorityQueue`,
15+
:class:`~queue.LifoQueue`), but it has no *timeout* parameter. The
16+
:func:`asyncio.wait_for` function can be used to cancel a task after a timeout.
17+
18+
Queue
19+
-----
20+
21+
.. class:: Queue(maxsize=0, \*, loop=None)
22+
23+
A queue, useful for coordinating producer and consumer coroutines.
24+
25+
If *maxsize* is less than or equal to zero, the queue size is infinite. If
26+
it is an integer greater than ``0``, then ``yield from put()`` will block
27+
when the queue reaches *maxsize*, until an item is removed by :meth:`get`.
28+
29+
Unlike the standard library :mod:`queue`, you can reliably know this Queue's
30+
size with :meth:`qsize`, since your single-threaded asyncio application won't
31+
be interrupted between calling :meth:`qsize` and doing an operation on the
32+
Queue.
33+
34+
.. versionchanged:: 3.4.3
35+
New :meth:`join` and :meth:`task_done` methods.
36+
37+
.. method:: empty()
38+
39+
Return ``True`` if the queue is empty, ``False`` otherwise.
40+
41+
.. method:: full()
42+
43+
Return ``True`` if there are :attr:`maxsize` items in the queue.
44+
45+
.. note::
46+
47+
If the Queue was initialized with ``maxsize=0`` (the default), then
48+
:meth:`full()` is never ``True``.
49+
50+
.. coroutinemethod:: get()
51+
52+
Remove and return an item from the queue. If queue is empty, wait until
53+
an item is available.
54+
55+
This method is a :ref:`coroutine <coroutine>`.
56+
57+
.. seealso::
58+
59+
The :meth:`empty` method.
60+
61+
.. method:: get_nowait()
62+
63+
Remove and return an item from the queue.
64+
65+
Return an item if one is immediately available, else raise
66+
:exc:`QueueEmpty`.
67+
68+
.. coroutinemethod:: join()
69+
70+
Block until all items in the queue have been gotten and processed.
71+
72+
The count of unfinished tasks goes up whenever an item is added to the
73+
queue. The count goes down whenever a consumer thread calls
74+
:meth:`task_done` to indicate that the item was retrieved and all work on
75+
it is complete. When the count of unfinished tasks drops to zero,
76+
:meth:`join` unblocks.
77+
78+
This method is a :ref:`coroutine <coroutine>`.
79+
80+
.. versionadded:: 3.4.3
81+
82+
.. coroutinemethod:: put(item)
83+
84+
Put an item into the queue. If the queue is full, wait until a free slot
85+
is available before adding item.
86+
87+
This method is a :ref:`coroutine <coroutine>`.
88+
89+
.. seealso::
90+
91+
The :meth:`full` method.
92+
93+
.. method:: put_nowait(item)
94+
95+
Put an item into the queue without blocking.
96+
97+
If no free slot is immediately available, raise :exc:`QueueFull`.
98+
99+
.. method:: qsize()
100+
101+
Number of items in the queue.
102+
103+
.. method:: task_done()
104+
105+
Indicate that a formerly enqueued task is complete.
106+
107+
Used by queue consumers. For each :meth:`~Queue.get` used to fetch a task, a
108+
subsequent call to :meth:`task_done` tells the queue that the processing
109+
on the task is complete.
110+
111+
If a :meth:`join` is currently blocking, it will resume when all items
112+
have been processed (meaning that a :meth:`task_done` call was received
113+
for every item that had been :meth:`~Queue.put` into the queue).
114+
115+
Raises :exc:`ValueError` if called more times than there were items
116+
placed in the queue.
117+
118+
.. versionadded:: 3.4.3
119+
120+
.. attribute:: maxsize
121+
122+
Number of items allowed in the queue.
123+
124+
125+
PriorityQueue
126+
-------------
127+
128+
.. class:: PriorityQueue
129+
130+
A subclass of :class:`Queue`; retrieves entries in priority order (lowest
131+
first).
132+
133+
Entries are typically tuples of the form: (priority number, data).
134+
135+
136+
LifoQueue
137+
---------
138+
139+
.. class:: LifoQueue
140+
141+
A subclass of :class:`Queue` that retrieves most recently added entries
142+
first.
143+
144+
145+
JoinableQueue
146+
^^^^^^^^^^^^^
147+
148+
.. class:: JoinableQueue
149+
150+
Deprecated alias for :class:`Queue`.
151+
152+
.. deprecated:: 3.4.3
153+
154+
155+
Exceptions
156+
^^^^^^^^^^
157+
158+
.. exception:: QueueEmpty
159+
160+
Exception raised when the :meth:`~Queue.get_nowait` method is called on a
161+
:class:`Queue` object which is empty.
162+
163+
164+
.. exception:: QueueFull
165+
166+
Exception raised when the :meth:`~Queue.put_nowait` method is called on a
167+
:class:`Queue` object which is full.

Doc/library/asyncio-sync.rst

Lines changed: 6 additions & 166 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,16 @@ Locks:
99
* :class:`Lock`
1010
* :class:`Event`
1111
* :class:`Condition`
12-
* :class:`Semaphore`
13-
* :class:`BoundedSemaphore`
1412

15-
Queues:
13+
Semaphores:
1614

17-
* :class:`Queue`
18-
* :class:`PriorityQueue`
19-
* :class:`LifoQueue`
20-
* :class:`JoinableQueue`
15+
* :class:`Semaphore`
16+
* :class:`BoundedSemaphore`
2117

22-
asyncio locks and queues API were designed to be close to classes of the
23-
:mod:`threading` module (:class:`~threading.Lock`, :class:`~threading.Event`,
18+
asyncio lock API was designed to be close to classes of the :mod:`threading`
19+
module (:class:`~threading.Lock`, :class:`~threading.Event`,
2420
:class:`~threading.Condition`, :class:`~threading.Semaphore`,
25-
:class:`~threading.BoundedSemaphore`) and the :mod:`queue` module
26-
(:class:`~queue.Queue`, :class:`~queue.PriorityQueue`,
27-
:class:`~queue.LifoQueue`), but they have no *timeout* parameter. The
21+
:class:`~threading.BoundedSemaphore`), but it has no *timeout* parameter. The
2822
:func:`asyncio.wait_for` function can be used to cancel a task after a timeout.
2923

3024
Locks
@@ -290,157 +284,3 @@ BoundedSemaphore
290284
This raises :exc:`ValueError` in :meth:`~Semaphore.release` if it would
291285
increase the value above the initial value.
292286

293-
294-
Queues
295-
------
296-
297-
Queue
298-
^^^^^
299-
300-
.. class:: Queue(maxsize=0, \*, loop=None)
301-
302-
A queue, useful for coordinating producer and consumer coroutines.
303-
304-
If *maxsize* is less than or equal to zero, the queue size is infinite. If
305-
it is an integer greater than ``0``, then ``yield from put()`` will block
306-
when the queue reaches *maxsize*, until an item is removed by :meth:`get`.
307-
308-
Unlike the standard library :mod:`queue`, you can reliably know this Queue's
309-
size with :meth:`qsize`, since your single-threaded asyncio application won't
310-
be interrupted between calling :meth:`qsize` and doing an operation on the
311-
Queue.
312-
313-
.. versionchanged:: 3.4.3
314-
New :meth:`join` and :meth:`task_done` methods.
315-
316-
.. method:: empty()
317-
318-
Return ``True`` if the queue is empty, ``False`` otherwise.
319-
320-
.. method:: full()
321-
322-
Return ``True`` if there are :attr:`maxsize` items in the queue.
323-
324-
.. note::
325-
326-
If the Queue was initialized with ``maxsize=0`` (the default), then
327-
:meth:`full()` is never ``True``.
328-
329-
.. coroutinemethod:: get()
330-
331-
Remove and return an item from the queue. If queue is empty, wait until
332-
an item is available.
333-
334-
This method is a :ref:`coroutine <coroutine>`.
335-
336-
.. seealso::
337-
338-
The :meth:`empty` method.
339-
340-
.. method:: get_nowait()
341-
342-
Remove and return an item from the queue.
343-
344-
Return an item if one is immediately available, else raise
345-
:exc:`QueueEmpty`.
346-
347-
.. coroutinemethod:: join()
348-
349-
Block until all items in the queue have been gotten and processed.
350-
351-
The count of unfinished tasks goes up whenever an item is added to the
352-
queue. The count goes down whenever a consumer thread calls
353-
:meth:`task_done` to indicate that the item was retrieved and all work on
354-
it is complete. When the count of unfinished tasks drops to zero,
355-
:meth:`join` unblocks.
356-
357-
This method is a :ref:`coroutine <coroutine>`.
358-
359-
.. versionadded:: 3.4.3
360-
361-
.. coroutinemethod:: put(item)
362-
363-
Put an item into the queue. If the queue is full, wait until a free slot
364-
is available before adding item.
365-
366-
This method is a :ref:`coroutine <coroutine>`.
367-
368-
.. seealso::
369-
370-
The :meth:`full` method.
371-
372-
.. method:: put_nowait(item)
373-
374-
Put an item into the queue without blocking.
375-
376-
If no free slot is immediately available, raise :exc:`QueueFull`.
377-
378-
.. method:: qsize()
379-
380-
Number of items in the queue.
381-
382-
.. method:: task_done()
383-
384-
Indicate that a formerly enqueued task is complete.
385-
386-
Used by queue consumers. For each :meth:`~Queue.get` used to fetch a task, a
387-
subsequent call to :meth:`task_done` tells the queue that the processing
388-
on the task is complete.
389-
390-
If a :meth:`join` is currently blocking, it will resume when all items
391-
have been processed (meaning that a :meth:`task_done` call was received
392-
for every item that had been :meth:`~Queue.put` into the queue).
393-
394-
Raises :exc:`ValueError` if called more times than there were items
395-
placed in the queue.
396-
397-
.. versionadded:: 3.4.3
398-
399-
.. attribute:: maxsize
400-
401-
Number of items allowed in the queue.
402-
403-
404-
PriorityQueue
405-
^^^^^^^^^^^^^
406-
407-
.. class:: PriorityQueue
408-
409-
A subclass of :class:`Queue`; retrieves entries in priority order (lowest
410-
first).
411-
412-
Entries are typically tuples of the form: (priority number, data).
413-
414-
415-
LifoQueue
416-
^^^^^^^^^
417-
418-
.. class:: LifoQueue
419-
420-
A subclass of :class:`Queue` that retrieves most recently added entries
421-
first.
422-
423-
424-
JoinableQueue
425-
^^^^^^^^^^^^^
426-
427-
.. class:: JoinableQueue
428-
429-
Deprecated alias for :class:`Queue`.
430-
431-
.. deprecated:: 3.4.3
432-
433-
434-
Exceptions
435-
^^^^^^^^^^
436-
437-
.. exception:: QueueEmpty
438-
439-
Exception raised when the :meth:`~Queue.get_nowait` method is called on a
440-
:class:`Queue` object which is empty.
441-
442-
443-
.. exception:: QueueFull
444-
445-
Exception raised when the :meth:`~Queue.put_nowait` method is called on a
446-
:class:`Queue` object which is full.

Doc/library/asyncio.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ Table of contents:
5858
asyncio-stream.rst
5959
asyncio-subprocess.rst
6060
asyncio-sync.rst
61+
asyncio-queue.rst
6162
asyncio-dev.rst
6263

6364
.. seealso::

0 commit comments

Comments
 (0)