2 min read

Python Queue Module

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.

Importing the Module

import queue

FIFO Queue (Queue)

The Queue class implements a basic first-in, first-out queue.

import queue

q = queue.Queue()

q.put(1)
q.put(2)
q.put(3)

print(q.get()) # Output: 1
print(q.get()) # Output: 2
print(q.get()) # Output: 3

LIFO Queue (LifoQueue)

The LifoQueue class implements a last-in, first-out queue (like a stack).

import queue

q = queue.LifoQueue()

q.put(1)
q.put(2)
q.put(3)

print(q.get()) # Output: 3
print(q.get()) # Output: 2
print(q.get()) # Output: 1

Priority Queue (PriorityQueue)

The PriorityQueue class retrieves entries in priority order (lowest first). Entries are typically tuples of the form (priority_number, data).

import queue

q = queue.PriorityQueue()

q.put((2, 'code'))
q.put((1, 'eat'))
q.put((3, 'sleep'))

while not q.empty():
    next_item = q.get()
    print(next_item)

# Output:
# (1, 'eat')
# (2, 'code')
# (3, 'sleep')

Blocking and Timeouts

put() and get() block by default if the queue is full (for put) or empty (for get). You can specify a timeout.

try:
    item = q.get(timeout=1)
except queue.Empty:
    print("Queue is empty")

Task Tracking

task_done() and join() are used to track when enqueued tasks are completed.

import threading
import queue

def worker():
    while True:
        item = q.get()
        print(f'Working on {item}')
        print(f'Finished {item}')
        q.task_done()

q = queue.Queue()

# Turn-on the worker thread
threading.Thread(target=worker, daemon=True).start()

# Send thirty task requests to the worker
for item in range(30):
    q.put(item)

# Block until all tasks are done
q.join()
print('All work completed')

programming/python/python