Reviewed by Aditya Kumar · Last reviewed 2026-03-24
**Why Priority Queues in Data Pipelines:** Task scheduling (Airflow, Celery), event processing (process by timestamp/priority), and merge of K sorted streams (heapq.merge). **Implementations:** (1) heapq: min-heap, O(log n) push/pop. (2) queue.PriorityQueue: thread-safe...
Why Priority Queues in Data Pipelines: Task scheduling (Airflow, Celery), event processing (process by timestamp/priority), and merge of K sorted streams (heapq.merge).
Implementations: (1) heapq: min-heap, O(log n) push/pop. (2) queue.PriorityQueue: thread-safe wrapper. (3) For dynamic priority updates: need decrease_key—heapq doesn't support; use lazy deletion (mark stale, ignore when popped) or custom Fibonacci heap.
Scalability: Single-process heap = millions of items. For distributed: use external queue (SQS, RabbitMQ) with priority. Celery uses Redis/RabbitMQ for priority queues—workers pull by priority.
import heapq
tasks = []
heapq.heappush(tasks, (priority, counter, task))
task = heapq.heappop(tasks) # lowest priority first
Red Flag: Comparing task objects when priorities tie (breaks heap). Pro-Move: 'We use (priority, monotonic_counter, task) so ties are broken by insertion order—deterministic.'
According to DataEngPrep.tech, this is one of the most frequently asked Python/Coding interview questions, reported at 1 company. DataEngPrep.tech maintains an editor-reviewed database of 1,863 data engineering interview questions across 7 categories.