18.5.8.Queues

Source code:Lib/asyncio/queues.py

Queues:

asyncio queue API was designed to be close to classes of thequeuemodule (Queue,PriorityQueue,LifoQueue), but it has notimeout parameter. Theasyncio.wait_for() function can be used to cancel a task after a timeout.

18.5.8.1.Queue

classasyncio.Queue(maxsize=0,*,loop=None)

A queue, useful for coordinating producer and consumer coroutines.

Ifmaxsize is less than or equal to zero, the queue size is infinite. Ifit is an integer greater than0, thenyieldfromput() will blockwhen the queue reachesmaxsize, until an item is removed byget().

Unlike the standard libraryqueue, you can reliably know this Queue’ssize withqsize(), since your single-threaded asyncio application won’tbe interrupted between callingqsize() and doing an operation on theQueue.

This class isnot thread safe.

Changed in version 3.4.4:Newjoin() andtask_done() methods.

empty()

ReturnTrue if the queue is empty,False otherwise.

full()

ReturnTrue if there aremaxsize items in the queue.

Note

If the Queue was initialized withmaxsize=0 (the default), thenfull() is neverTrue.

coroutineget()

Remove and return an item from the queue. If queue is empty, wait untilan item is available.

This method is acoroutine.

See also

Theempty() method.

get_nowait()

Remove and return an item from the queue.

Return an item if one is immediately available, else raiseQueueEmpty.

coroutinejoin()

Block until all items in the queue have been gotten and processed.

The count of unfinished tasks goes up whenever an item is added to thequeue. The count goes down whenever a consumer thread callstask_done() to indicate that the item was retrieved and all work onit is complete. When the count of unfinished tasks drops to zero,join() unblocks.

This method is acoroutine.

New in version 3.4.4.

coroutineput(item)

Put an item into the queue. If the queue is full, wait until a free slotis available before adding item.

This method is acoroutine.

See also

Thefull() method.

put_nowait(item)

Put an item into the queue without blocking.

If no free slot is immediately available, raiseQueueFull.

qsize()

Number of items in the queue.

task_done()

Indicate that a formerly enqueued task is complete.

Used by queue consumers. For eachget() used to fetch a task, asubsequent call totask_done() tells the queue that the processingon the task is complete.

If ajoin() is currently blocking, it will resume when all itemshave been processed (meaning that atask_done() call was receivedfor every item that had beenput() into the queue).

RaisesValueError if called more times than there were itemsplaced in the queue.

New in version 3.4.4.

maxsize

Number of items allowed in the queue.

18.5.8.2.PriorityQueue

classasyncio.PriorityQueue

A subclass ofQueue; retrieves entries in priority order (lowestfirst).

Entries are typically tuples of the form: (priority number, data).

18.5.8.3.LifoQueue

classasyncio.LifoQueue

A subclass ofQueue that retrieves most recently added entriesfirst.

18.5.8.3.1.Exceptions

exceptionasyncio.QueueEmpty

Exception raised when theget_nowait() method is called on aQueue object which is empty.

exceptionasyncio.QueueFull

Exception raised when theput_nowait() method is called on aQueue object which is full.