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 - Conclusion: It is clear from the time profiling that, heapq runs faster than PriorityQueue function. And this is obvious because PriorityQueue uses the threading module to implement a mutex structure for thread safety while manipulating items ...
Discussions

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
Removing an arbitrary element from a priority queue
What's the key for self.indices? Is it a patient object or a patient name? More on reddit.com
🌐 r/learnpython
3
9
October 27, 2017
🌐
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 ...
🌐
Stackify
stackify.com › a-guide-to-python-priority-queue
A Guide to Python Priority Queue - Stackify
February 18, 2025 - Limit Queue Size: For both implementations, limiting the queue size using maxsize in PriorityQueue or implementing manual bounds for heapq can help manage memory and prevent overflows. By understanding the differences and best practices, you can choose the right implementation and optimize performance for your Python applications.
🌐
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.
🌐
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.
🌐
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, ...
Find elsewhere
🌐
Python
docs.python.org › 3 › library › heapq.html
heapq — Heap queue algorithm
Source code: Lib/heapq.py This module provides an implementation of the heap queue algorithm, also known as the priority queue algorithm. Min-heaps are binary trees for which every parent node has ...
🌐
Gitbook
sisyphus.gitbook.io › project › python-notes › python-priority-queue-heapq
Python priority queue -- heapq - The Truth of Sisyphus - GitBook
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, val) # Push the value item onto the heap, maintaining the heap invariant elif val > self.window[0]: # To access the smallest item without popping it, use heap[0] heapq.heapreplace(self.window, val) # This heapreplace operation is more efficient than a heappop() followed by heappush() and can be more appropriate when using a fixed-size heap.
🌐
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 ... provides heaps and priority queues through two powerful modules: heapq for lightweight, efficient heaps, and queue.PriorityQueue for thread-safe operations....
🌐
Built In
builtin.com › data-science › priority-queues-in-python
Introduction to Priority Queues in Python | Built In
Summary: A priority queue in Python allows elements to be processed based on assigned priority rather than arrival order. It can be implemented using lists, the heapq module for efficiency, or the thread-safe PriorityQueue class for concurrent ...
🌐
Gitbook
sisyphus.gitbook.io › project › python-notes › heapq-vs.-q.priorityqueue
Heapq vs. Q.PriorityQueue - The Truth of Sisyphus - GitBook
Queue.PriorityQueue is a thread-safe class, while the heapqmodule makes no thread-safety guarantees. From the Queuemodule documentation: The Queue module implements multi-producer, multi-consumer queues.
🌐
Programmathically
programmathically.com › home › software design › priority queue and heapq in python
Priority Queue and Heapq in Python - Programmathically
December 10, 2021 - The Python priority queue from the queue module is based on a binary heap from the heapq module. Contrary to a direct implementation based on heapq, the Python priority queue ensures thread safety.
🌐
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 - Min-Heap: [5, 10, 30, 20, 15] After ... priority element is dequeued first. Unlike normal queues (FIFO), priority queues focus on priority-based ordering. Python offers multiple ways to implement priority queues:...
🌐
Towards Data Science
towardsdatascience.com › home › latest › priority queues in python
Priority Queues in Python | Towards Data Science
January 26, 2025 - In this post, we will discuss the implementation of a priority queue in python using a heap data structure. Let’s get started! It is worth familiarizing ourselves with the python ‘heapq’ module before we build our ‘PriorityQueue’ class.
🌐
Reddit
reddit.com › r/python › is there a better priority queue?
r/Python on Reddit: Is there a better priority queue?
November 21, 2017 -

I know python has heapq and queue.priorityqueue but honestly, both of them are really cumbersome compared to Java's priorityqueue. I find it tedious to have to insert a tuple, with the first element in the tuple defining the priority. Also, it makes it hard to write more complex comparisons. Is there a way we can pass in a comparator to the Priorityqueue? I know it's possible to define classes with their own comparator method, but again, this is really tedious and I'm looking for something as close as possible to Java's PQ.

🌐
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 ·
🌐
Hostman
hostman.com › tutorials › implementing a priority queue in python
Implementing a Priority Queue in Python: A Comprehensive Guide
December 29, 2025 - In this section, we will look closer at advanced usage of priority queues. Updating an element's priority involves removing the element and adding it again with the new priority. This can be inefficient, but it's a necessary step since heapq does not support direct priority updates.
Price   $
Address   1999 Harrison St 1800 9079, 94612, Oakland
🌐
iO Flood
ioflood.com › blog › python-priority-queue-practical-guide-with-examples
Python Priority Queue Examples | Best Practices and Usage
July 8, 2024 - Conversely, PriorityQueue is a higher-level abstraction that can employ a Heap as its underlying data structure but incorporates additional features, such as thread safety. Python also offers a heapq module, implementing the heap queue algorithm, ...
🌐
Bogotobogo
bogotobogo.com › python › python_PriorityQueue_heapq_Data_Structure.php
Python Tutorial: Data Structure - Priority Queue & heapq - 2020
A priority queue is an abstract concept like a list or a map; just as a list can be implemented with a linked list or an array, a priority queue can be implemented with a heap or a variety of other methods such as an unordered array.