Queues¶
Source code: :source:`Lib/asyncio/queues.py`
Queues:
asyncio queue API was designed to be close to classes of the queue
module (Queue
, PriorityQueue
,
LifoQueue
), but it has no timeout parameter. The
asyncio.wait_for()
function can be used to cancel a task after a timeout.
Queue¶
-
class
asyncio.
Queue
(maxsize=0, *, loop=None)¶ A queue, useful for coordinating producer and consumer coroutines.
If maxsize is less than or equal to zero, the queue size is infinite. If it is an integer greater than
0
, thenawait put()
will block when the queue reaches maxsize, until an item is removed byget()
.Unlike the standard library
queue
, you can reliably know this Queue’s size withqsize()
, since your single-threaded asyncio application won’t be interrupted between callingqsize()
and doing an operation on the Queue.This class is not thread safe.
Changed in version 3.4.4: New
join()
andtask_done()
methods.-
empty
()¶ Return
True
if the queue is empty,False
otherwise.
-
full
()¶ Return
True
if there aremaxsize
items in the queue.Note
If the Queue was initialized with
maxsize=0
(the default), thenfull()
is neverTrue
.
-
get_nowait
()¶ Remove and return an item from the queue.
Return an item if one is immediately available, else raise
QueueEmpty
.
-
put_nowait
(item)¶ Put an item into the queue without blocking.
If no free slot is immediately available, raise
QueueFull
.
-
qsize
()¶ Number of items in the queue.
-
task_done
()¶ Indicate that a formerly enqueued task is complete.
Used by queue consumers. For each
get()
used to fetch a task, a subsequent call totask_done()
tells the queue that the processing on the task is complete.If a
join()
is currently blocking, it will resume when all items have been processed (meaning that atask_done()
call was received for every item that had beenput()
into the queue).Raises
ValueError
if called more times than there were items placed in the queue.New in version 3.4.4.
-
maxsize
¶ Number of items allowed in the queue.
-
PriorityQueue¶
LifoQueue¶
Exceptions¶
-
exception
asyncio.
QueueEmpty
¶ Exception raised when the
get_nowait()
method is called on aQueue
object which is empty.
-
exception
asyncio.
QueueFull
¶ Exception raised when the
put_nowait()
method is called on aQueue
object which is full.