Queue¶

Queue implements FIFO with multiple data producers and multiple consumers. It's particularly useful in multithreading, allowing for correct information exchange between threads. There are also LifoQueue for LIFO implementation and PriorityQueue for implementing a priority queue.

In [5]:
from queue import Queue
q = Queue(maxsize=1000)

q.put('eat', block=True, timeout=10)
q.put('sleep')  # By default block=True, timeout=None
q.put('code')
q.put_nowait('repeat')  # Equivalent to put('repeat', block=False). If a free slot is not immediately available, a queue.Full exception will be raised
print(q.queue)

a = q.get(block=True, timeout=10)  # Remove and return an element from FIFO
b = q.get()  # By default block=True, timeout=None
c = q.get_nowait()  # Equivalent to get(False)
print(a, b, c, q.queue)
deque(['eat', 'sleep', 'code', 'repeat'])
eat sleep code deque(['repeat'])