Skip to content

Queue

The queue module implements multi-producer, multi-consumer queues. It is especially useful in threaded programming when information must be exchanged safely between multiple threads. The Queue class in this module implements all the required locking semantics.

Queue class

class Queue

class Queue(maxsize=0)

Constructor for a FIFO queue. maxsize is an integer that sets the upperbound limit on the number of items that can be placed in the queue. Insertion will block once this size has been reached, until queue items are consumed. If maxsize is less than or equal to zero, the queue size is infinite.

Queue.qsize

qsize()

Return the approximate size of the queue. Note, qsize() > 0 doesn’t guarantee that a subsequent get() will not block, nor will qsize() < maxsize guarantee that put() will not block.

Queue.full

full()

Return True if the queue is full, False otherwise. If full() returns True it doesn’t guarantee that a subsequent call to get() will not block. Similarly, if full() returns False it doesn’t guarantee that a subsequent call to put() will not block.

Queue.empty

empty()

Return True if the queue is empty, False otherwise. If empty() returns True it doesn’t guarantee that a subsequent call to put() will not block. Similarly, if empty() returns False it doesn’t guarantee that a subsequent call to get() will not block.

Queue.put

put(obj, block=True, timeout=-1)

Insert obj into the queue. If the queue is full, and block is True, block until a free slot becomes available. If block is False, raise QueueFull. If timeout is greater than zero, waits for the specified amount of milliseconds before raising QueueFull exception.

Queue.get

get()

Remove and return an object out of the queue. If the queue is empty, block until an item is available.

Queue.peek

peek()

Return the object at the head of the queue without removing it. If the queue is empty, wait until an item is available.

Queue.clear

clear()

Clear the queue by removing all elements.