queue
The Python queue
module provides reliable thread-safe implementations of the queue data structure. It is commonly used for task scheduling and managing work between multiple threads.
Here’s a quick example:
>>> import queue
>>> tasks = queue.Queue()
>>> tasks.put("task1")
>>> tasks.put("task2")
>>> tasks.get()
'task1'
>>> tasks.get()
'task2'
Key Features
- Provides thread-safe FIFO, LIFO, and priority queues
- Supports blocking and non-blocking operations
- Allows a maximum size to be set for queues
Frequently Used Classes and Functions
Object | Type | Description |
---|---|---|
queue.Queue |
Class | Provides a first-in, first-out (FIFO) queue |
queue.Queue.put() |
Method | Adds an item to the queue |
queue.Queue.get() |
Method | Removes and returns an item from the queue |
queue.Queue.qsize() |
Method | Returns the approximate size of the queue |
queue.Queue.empty() |
Method | Checks whether the queue is empty |
queue.Queue.full() |
Method | Checks whether the queue is full |
queue.LifoQueue |
Class | Provides a last-in, first-out (LIFO) queue |
queue.PriorityQueue |
Class | Provides a queue where entries are retrieved by priority |
Examples
Using a LIFO queue:
>>> from queue import LifoQueue
>>> q = LifoQueue()
>>> q.put("task1")
>>> q.put("task2")
>>> q.get()
'task2'
Using a priority queue:
>>> from queue import PriorityQueue
>>> q = PriorityQueue()
>>> q.put((2, "task1"))
>>> q.put((1, "task2"))
>>> q.get()
(1, 'task2')
Common Use Cases
- Managing task scheduling in multi-threaded applications
- Implementing producer-consumer patterns
- Prioritizing tasks using priority queues
Real-World Example
Consider a scenario where multiple worker threads need to process tasks concurrently. In this situation, you can use the queue
module to manage these tasks efficiently:
workers.py
import logging
import queue
import threading
import time
logging.basicConfig(level=logging.INFO, format="%(threadName)s %(message)s")
def worker(tasks):
while True:
task = tasks.get()
if task is None:
tasks.task_done()
break
logging.info(f"processing {task}")
time.sleep(1)
tasks.task_done()
tasks = queue.Queue()
for task in ["task1", "task2", "task3", "task4", "task5"]:
tasks.put(task)
num_workers = 2
for _ in range(num_workers):
tasks.put(None)
threads = [
threading.Thread(target=worker, args=(tasks,)) for _ in range(num_workers)
]
for thread in threads:
thread.start()
tasks.join()
for thread in threads:
thread.join()
# Output:
# Thread-1 (worker) processing task1
# Thread-2 (worker) processing task2
# Thread-1 (worker) processing task3
# Thread-2 (worker) processing task4
# Thread-1 (worker) processing task5
In this example, you use the queue
module to distribute tasks among two worker threads, ensuring that each task is processed exactly once.
Related Resources
Tutorial
Python Stacks, Queues, and Priority Queues in Practice
In this tutorial, you'll take a deep dive into the theory and practice of queues in programming. Along the way, you'll get to know the different types of queues, implement them, and then learn about the higher-level queues in Python's standard library. Be prepared to do a lot of coding.
For additional information on related topics, take a look at the following resources:
By Leodanis Pozo Ramos • Updated July 17, 2025