API Reference#

exception QueueException#

Base class of all the queue-related exceptions.

exception WouldBlock#

Raised by X_nowait functions if X would block.

exception Closed#

Occurs when:

  • one tries to get an item from a queue that is in the CLOSED state.

  • one tries to get an item from an empty queue that is in the HALF_CLOSED state.

  • one tries to put an item into a queue that is in the CLOSED or HALF_CLOSED state.

class Queue(*, capacity: int | None = None, order: Literal['fifo', 'lifo', 'small-first'] = 'fifo')#
Parameters:

capacity – Cannot be zero. Unlimited if None.

property size: int#

Number of items in the queue. This equals to len(queue).

property capacity: int | None#

Number of items allowed in the queue. None if unbounded.

property is_empty: bool#
property is_full: bool#
property order: Literal['fifo', 'lifo', 'small-first']#
get() Awaitable[Any]#
item = await queue.get()
get_nowait() Any#
item = queue.get_nowait()
put(item) Awaitable#
await queue.put(item)
put_nowait(item)#
queue.put_nowait(item)
half_close()#

Partially closes the queue. No further putting-opeations are allowed.

close()#

Fully closes the queue. No further putting/getting-operations are allowed. All items in the queue are discarded.

async __aiter__()#

Keeps retrieving items from the queue until it is closed.

async for item in queue:
    ...

which is equivalent to:

try:
    while True:
        item = await queue.get()
        ...
except Closed:
    pass
class QueueState#

Enum class that represents the Queue state.

OPENED#

All operations are allowed.

HALF_CLOSED#

Putting-operations are not allowed.

CLOSED#

Putting/getting-operations are not allowed.