PYTHON
Implementing Priority Queues with `heapq`
Discover how to use Python's `heapq` module to implement efficient priority queues, crucial for managing scheduled tasks or prioritizing events in web services.
import heapq
# A min-heap implementation using a list
priority_queue = []
# Add tasks to the priority queue. Items are tuples: (priority, task_name)
# Lower priority numbers mean higher priority (min-heap property)
heapq.heappush(priority_queue, (3, 'Low priority task A'))
heapq.heappush(priority_queue, (1, 'High priority task C'))
heapq.heappush(priority_queue, (2, 'Medium priority task B'))
heapq.heappush(priority_queue, (1, 'High priority task D')) # Same priority as C
print(f"Queue after pushes: {priority_queue}") # Not sorted visually, but heap property maintained
# Retrieve and remove the highest priority (lowest number) item
priority, task = heapq.heappop(priority_queue)
print(f"Processed highest priority task: {task} (Priority: {priority})") # Output: Processed highest priority task: High priority task C (Priority: 1)
priority, task = heapq.heappop(priority_queue)
print(f"Processed highest priority task: {task} (Priority: {priority})") # Output: Processed highest priority task: High priority task D (Priority: 1)
# You can also use objects if they are comparable or define a __lt__ method
class Job:
def __init__(self, priority, description):
self.priority = priority
self.description = description
def __lt__(self, other): # Define less than for comparison
return self.priority < other.priority
def __repr__(self):
return f"Job(priority={self.priority}, description='{self.description}')"
job_queue = []
heapq.heappush(job_queue, Job(5, 'Background cleanup'))
heapq.heappush(job_queue, Job(1, 'Urgent user email'))
heapq.heappush(job_queue, Job(3, 'Analytics report generation'))
print(f"
Job queue: {job_queue}")
processed_job = heapq.heappop(job_queue)
print(f"Processed job: {processed_job}") # Output: Processed job: Job(priority=1, description='Urgent user email')
How it works: The `heapq` module in Python provides an implementation of the heap queue algorithm, also known as the priority queue algorithm. It allows you to add and retrieve the smallest item (highest priority) efficiently. This is invaluable in web development for scenarios such as managing background tasks by their priority, scheduling events, or processing incoming requests where some have higher precedence. The primary operations `heappush` and `heappop` maintain the heap invariant, ensuring that the smallest element is always at the root.