Queue.PriorityQueue is a thread-safe class, while the heapq module makes no thread-safety guarantees. From the Queue module documentation:

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. The Queue class in this module implements all the required locking semantics. It depends on the availability of thread support in Python; see the threading module.

The heapq module offers no locking, and operates on standard list objects, which are not meant to be thread-safe.

In fact, the PriorityQueue implementation uses heapq under the hood to do all prioritisation work, with the base Queue class providing the locking to make this thread-safe. See the source code for details.

This makes the heapq module faster; there is no locking overhead. In addition, you are free to use the various heapq functions in different, novel ways, the PriorityQueue only offers the straight-up queueing functionality.

Answer from Martijn Pieters on Stack Overflow
Top answer
1 of 3
128

Queue.PriorityQueue is a thread-safe class, while the heapq module makes no thread-safety guarantees. From the Queue module documentation:

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. The Queue class in this module implements all the required locking semantics. It depends on the availability of thread support in Python; see the threading module.

The heapq module offers no locking, and operates on standard list objects, which are not meant to be thread-safe.

In fact, the PriorityQueue implementation uses heapq under the hood to do all prioritisation work, with the base Queue class providing the locking to make this thread-safe. See the source code for details.

This makes the heapq module faster; there is no locking overhead. In addition, you are free to use the various heapq functions in different, novel ways, the PriorityQueue only offers the straight-up queueing functionality.

2 of 3
30

queue.PriorityQueue is a partial wrapper around the heapq class.

In other words, a queue.PriorityQueue is actually a heapq, placed in the queue module with a couple of renamed methods to make the heapq easier to use, much like a regular queue.

In heapq, you use use the method heappush() to add a new item and the method heappop() to remove one. That is not very queue-like, so queue.PriorityQueue let you use the usual queue methods such as put and get to do the same thing.

There are some features of heapq that are not carried over into queue.PriorityQueue, such as heappushpop() and heapreplace(), but you are less likely to use those. If you need them (and I do in my current project), perhaps you should use heapq rather than queue.PriorityQueue.

Also, since heapq is specialized for its purpose, it is not thread safe (as noted in another answer here.)

🌐
GeeksforGeeks
geeksforgeeks.org › python › difference-between-heapq-and-priorityqueue-in-python
Difference Between heapq and PriorityQueue in Python - GeeksforGeeks
July 23, 2025 - heapq works on the principle of binary heap, while PriorityQueue works on the basis of queue data structure and can be assigned a priority.
Discussions

Is a heap the same thing as a priority queue?
Typical CS student: this is so easy kys Typical CompE major: yeah basically. But wtf is a hash map? More on reddit.com
🌐 r/Purdue
12
43
April 23, 2023
Is there a better priority queue?
You don't have to give a priority queue a tuple of (priority, value), that's just the most common use case. If you wanted to base priority on 3 variables, you could give (a, b, c, value), where ties between a values are broken by the b values, etc. More on reddit.com
🌐 r/Python
5
6
November 21, 2017
Peek method in Priority Queue?
The heapq object is a list. So you can peek with standard indexing. I don't think there's a way to peek on a queue.Queue, just due to how they are implemented. More on reddit.com
🌐 r/learnpython
2
1
September 26, 2020
How to "decrease priority" in a min-priority queue in Dijkstra's algorithm?
This depends on how the priority queue is implemented, as there are several ways of achieving this in logarithmic time. For example, if implemented as a balanced BST (binary search tree), all lookups, insertions, and removals are logarithmic. Hence decrease priority could just be implemented as a removal followed by an insertion (of the old and new values). More on reddit.com
🌐 r/algorithms
11
18
January 10, 2022
🌐
Built In
builtin.com › data-science › priority-queues-in-python
Introduction to Priority Queues in Python | Built In
First, it’s synchronized, so it supports concurrent processes. Second, it’s a class interface as opposed to the function-based interface of heapq. Thus, PriorityQueue is the classic object-oriented programming (OOP) style of implementing and using priority queues.
🌐
Gitbook
sisyphus.gitbook.io › project › python-notes › heapq-vs.-q.priorityqueue
Heapq vs. Q.PriorityQueue - The Truth of Sisyphus - GitBook
The heapq module offers no locking, and operates on standard list objects, which are not meant to be thread-safe. In fact, the PriorityQueue implementation uses heapq under the hood to do all prioritisation work, with the base Queue class providing ...
🌐
Medium
medium.com › @2019077_13406 › priority-queue-v-s-heapq-python-a4858c0191ac
Priority Queue V/s Heapq Python. Lately, I have been solving LeetCode… | by kartik chuphal | Medium
December 15, 2024 - Upon reviewing the implementation, I found that PriorityQueue is essentially a thread-safe wrapper for heapq, which justifies why it’s slower. And folks, I encourage you to try implementing a priority queue from scratch — I’m planning to do the same after publishing this article!
🌐
DigitalOcean
digitalocean.com › community › tutorials › priority-queue-python
How to Use a Priority Queue in Python | DigitalOcean
July 11, 2025 - In summary, heapq is preferred for single-threaded applications where performance is paramount, while PriorityQueue is ideal for multithreaded applications where thread safety and synchronization are crucial. Furthermore, this tutorial has addressed some common questions about priority queues, ...
🌐
LeetCode
leetcode.com › discuss › general-discussion › 925326 › queuepriorityqueue-vs-heapq-for-priority-queues-in-python
Queue.PriorityQueue vs heapq for priority queues in python? - Discuss - LeetCode
Queue.PriorityQueue vs heapq for priority queues in python? Anonymous User · 1501 · Nov 06, 2020 · Which do you prefer to use in interviews? https://docs.python.org/3/library/heapq.html · import heapq H = [21,1,45,78,3,5] # Create the heap heapq.heapify(H) print(H) # Remove element from the heap heapq.heappop(H) https://docs.python.org/3/library/queue.html#queue.PriorityQueue ·
Find elsewhere
🌐
Real Python
realpython.com › python-heapq-module
The Python heapq Module: Using Heaps and Priority Queues – Real Python
July 18, 2022 - Get the Source Code: Click here to get the source code you’ll use to learn about the Python heapq module in this tutorial. Heaps are concrete data structures, whereas priority queues are abstract data structures.
🌐
Stackify
stackify.com › a-guide-to-python-priority-queue
A Guide to Python Priority Queue - Stackify
February 18, 2025 - This module is built around the ... element is always at the root. While queue.PriorityQueue is a higher-level implementation, heapq offers flexibility and lower-level control, making it an excellent choice for custom priority ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › heap-and-priority-queue-using-heapq-module-in-python
Heap and Priority Queue using heapq module in Python - GeeksforGeeks
July 23, 2025 - If two elements have the same priority, then they appear in the order in which they appear in the queue. Heapq module is an implementation of heap queue algorithm (priority queue algorithm) in which the property of min-heap is preserved.
🌐
Developer Indian
developerindian.com › articles › heap-and-priority-queue-in-python-a-complete-guide-with-examples
Heap and Priority Queue in Python: A Complete Guide with Examples
August 16, 2025 - Use PriorityQueue when working with multi-threaded programs. Always store priority as the first element of a tuple for proper ordering. For max-heap, store negative values since Python’s heapq is min-heap by default.
🌐
Real Python
realpython.com › lessons › heapq-and-priorityqueue
heapq and PriorityQueue (Video) – Real Python
It has the same performance and restrictions of heapq, but also uses locks to ensure its methods are atomic. Similar to the other objects in the queue library, PriorityQueue uses .put(), .get(), and .get_nowait().
Published   May 11, 2021
🌐
iO Flood
ioflood.com › blog › python-priority-queue-practical-guide-with-examples
Python Priority Queue Examples | Best Practices and Usage
July 8, 2024 - While the heapq module does not provide the higher-level features of the PriorityQueue class, like thread safety, it delivers more control over the underlying data structure and can be more efficient for certain use cases.
🌐
CodeSignal
codesignal.com › learn › courses › interview-prep-the-last-mile-in-python › lessons › heap-and-priority-queue-in-python
Heap and Priority Queue in Python
For example, if you want to find the n-th largest number in a list, using sorting can be costly. By leveraging Python's heapq.nlargest function, the heap data structure lets us do this efficiently. ... Priority queues are an abstraction over heaps that store elements according to their priorities.
🌐
Python
docs.python.org › 3 › library › heapq.html
heapq — Heap queue algorithm
Raise KeyError if not found.' entry = entry_finder.pop(task) entry[-1] = REMOVED def pop_task(): 'Remove and return the lowest priority task. Raise KeyError if empty.' while pq: priority, count, task = heappop(pq) if task is not REMOVED: del entry_finder[task] return task raise KeyError('pop from an empty priority queue')
🌐
Caasify
caasify.com › home › blog › master priority queue in python: use heapq and queue.priorityqueue
Master Priority Queue in Python: Use heapq and queue.PriorityQueue | Caasify
October 5, 2025 - In Python, the queue.PriorityQueue class provides a thread-safe priority queue implementation. Built on top of Python’s heapq module, this class adds an important feature: it allows multiple threads to safely access and modify the queue at ...
🌐
Gitbook
sisyphus.gitbook.io › project › python-notes › python-priority-queue-heapq
Python priority queue -- heapq - The Truth of Sisyphus - GitBook
class KthLargest(object): def __init__(self, k, nums): self.window = nums self.k = k heapq.heapify(self.window) # Transform list x into a heap, in-place, in linear time. while len(self.window) > k: heapq.heappop(self.window) # Pop and return the smallest item from the heap, maintaining the heap invariant. To access the smallest item without popping it, use heap[0]. # keep a K-size priority queue (heapq in python), and always make it updated and return the smallest of this group, which will be the k-th large element def add(self, val): if len(self.window) < self.k: heapq.heappush(self.window, v
🌐
Medium
medium.com › @balakrishnamaduru › heap-and-priority-queue-in-python-mastering-heapq-and-queue-priorityqueue-bdab14488b43
Heap and Priority Queue in Python: Mastering heapq and queue.PriorityQueue | by Balakrishna Maduru | Medium
November 23, 2024 - Efficient task scheduling, finding ... two powerful modules: heapq for lightweight, efficient heaps, and queue.PriorityQueue for thread-safe operations....
🌐
Verve AI
vervecopilot.com › interview-questions › can-priority-queue-implementation-python-be-the-secret-weapon-for-acing-your-next-interview
Can Priority Queue Implementation Python Be The Secret Weapon For Acing Your Next Interview
Q: Is queue.PriorityQueue always the best choice for priority queue implementation python? A: Not always. It's thread-safe and easy, but heapq offers more control and better performance for single-threaded applications.
🌐
Just2good
aoc.just2good.co.uk › python › priority_queues.html
Priority Queues and Heaps | Learning Python with Advent of Code Walkthroughs
Recall that the heapq always pops the item with the smallest value. Thus, highest priority = smallest value. Bear this in mind when adding objects to a heapq. An alternative way to add things to a queue with our desired priority is to add them as the second element of a tuple.