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