Python Queue Module Explained: Thread-Safe Queue Classes
An in-depth guide to Python's queue module, covering thread-safe queue classes, their usage in multithreaded programming, common methods like put, get, task_done, join, and practical code examples demonstrating producer-consumer patterns and bounded queues.
Python's queue module provides thread-safe queue classes for safe data sharing between threads.
In multithreaded programming, using global variables can cause race conditions; queues simplify synchronization.
The module offers several queue types: Queue (FIFO), LifoQueue (LIFO), and PriorityQueue.
Key methods include put(), get(), task_done(), join(), qsize(), empty(), full(), put_nowait(), get_nowait().
Examples demonstrate basic usage, bounded queues with maxsize, handling full/empty exceptions, and producer-consumer patterns.
import queue
import threading
import time
def producer(q):
for i in range(5):
q.put(i)
print(f'Produced {i}')
time.sleep(0.1)
q.put(None) # sentinel
def consumer(q):
while True:
item = q.get()
if item is None:
break
print(f'Consumed {item}')
q.task_done()
q = queue.Queue()
t1 = threading.Thread(target=producer, args=(q,))
t2 = threading.Thread(target=consumer, args=(q,))
t1.start()
t2.start()
t1.join()
t2.join()
q.join()
print('All tasks completed')Additional examples show put_nowait/get_nowait and handling Full and Empty exceptions:
import queue
q = queue.Queue(maxsize=2)
try:
q.put_nowait(1)
q.put_nowait(2)
q.put_nowait(3) # raises Full
except queue.Full:
print('Queue is full')
while not q.empty():
print(q.get())Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
