18.5.8. Filas

Código-fonte: 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.

18.5.8.1. 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, then yield from put() will block when the queue reaches maxsize, until an item is removed by get().

Unlike the standard library queue, you can reliably know this Queue’s size with qsize(), since your single-threaded asyncio application won’t be interrupted between calling qsize() and doing an operation on the Queue.

Esta classe não é seguro para thread.

Alterado na versão 3.4.4: New join() and task_done() methods.

empty()

Retorna True se a fila estiver vazia, False caso contrário.

full()

Retorna True se existem maxsize itens na fila.

Nota

If the Queue was initialized with maxsize=0 (the default), then full() is never True.

coroutine get()

Remove e retorna um item da fila. Se a fila estiver vazia, aguarda até que um item esteja disponível.

This method is a coroutine.

Ver também

The empty() method.

get_nowait()

Remove and return an item from the queue.

Retorna um item se houver um imediatamente disponível, caso contrário levanta QueueEmpty.

coroutine join()

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 the queue. The count goes down whenever a consumer thread calls task_done() to indicate that the item was retrieved and all work on it is complete. When the count of unfinished tasks drops to zero, join() unblocks.

This method is a coroutine.

Novo na versão 3.4.4.

coroutine put(item)

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

This method is a coroutine.

Ver também

The full() method.

put_nowait(item)

Coloca um item na fila sem bloqueá-la.

Se nenhuma posição livre estiver imediatamente disponível, levanta QueueFull.

qsize()

Number of items in the queue.

task_done()

Indica que a tarefa anteriormente enfileira está concluída.

Usada por fila de consumidores. Para cada get() usado para buscar uma tarefa, uma chamada subsequente para task_done() avisa a fila, que o processamento na tarefa está concluído.

Se um join() estiver sendo bloqueado no momento, ele irá continuar quando todos os itens tiverem sido processados (significando que uma chamada task_done() foi recebida para cada item que foi chamado o método put() para colocar na fila).

Levanta ValueError se chamada mais vezes do que a quantidade de itens existentes na fila.

Novo na versão 3.4.4.

maxsize

Número de itens permitidos na fila.

18.5.8.2. PriorityQueue

class asyncio.PriorityQueue

A subclass of Queue; retrieves entries in priority order (lowest first).

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

18.5.8.3. LifoQueue

class asyncio.LifoQueue

A subclass of Queue that retrieves most recently added entries first.

18.5.8.3.1. Exceções

exception asyncio.QueueEmpty

Exception raised when the get_nowait() method is called on a Queue object which is empty.

exception asyncio.QueueFull

Exception raised when the put_nowait() method is called on a Queue object which is full.