-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
gh-116738: Make _heapq module thread-safe #135036
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ec3cade
gh-116738: Make _heapq module thread-safe
yoney 01b2be4
gh-116738: Add news entry in Misc/NEWS/next
yoney 68f3a26
gh-116738: Address the review comments
yoney 41d145a
gh-116738: Address the review comments
yoney a22da8f
gh-116738: Remove type hints
yoney 75a1d3a
gh-116738: Address the review comments
yoney e8138be
gh-116738: Add NULL check for the item arg in heappush()
yoney 3f09689
gh-116738: Fix typo
yoney 7ffdb6d
gh-116738: Use invariant checks from heapq test
yoney c2225a5
gh-116738: Remove forgotten type hint
yoney a196a00
Merge branch 'main' into ft_heapq
mpage File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,279 @@ | ||
import unittest | ||
|
||
import heapq | ||
import operator | ||
|
||
from enum import Enum | ||
from threading import Thread, Barrier | ||
from random import shuffle, randint | ||
|
||
from test.support import threading_helper | ||
|
||
|
||
NTHREADS: int = 10 | ||
OBJECT_COUNT: int = 5_000 | ||
|
||
|
||
class HeapKind(Enum): | ||
MIN = 1 | ||
MAX = 2 | ||
|
||
|
||
@threading_helper.requires_working_threading() | ||
class TestHeapq(unittest.TestCase): | ||
def test_racing_heapify(self): | ||
heap = list(range(OBJECT_COUNT)) | ||
shuffle(heap) | ||
|
||
def heapify_func(heap: list[int]): | ||
heapq.heapify(heap) | ||
|
||
self.run_concurrently( | ||
worker_func=heapify_func, args=(heap,), nthreads=NTHREADS | ||
) | ||
self.assertTrue(self.is_min_heap_property_satisfied(heap)) | ||
|
||
def test_racing_heappush(self): | ||
heap = [] | ||
|
||
def heappush_func(heap: list[int]): | ||
for item in reversed(range(OBJECT_COUNT)): | ||
heapq.heappush(heap, item) | ||
|
||
self.run_concurrently( | ||
worker_func=heappush_func, args=(heap,), nthreads=NTHREADS | ||
) | ||
self.assertTrue(self.is_min_heap_property_satisfied(heap)) | ||
|
||
def test_racing_heappop(self): | ||
heap = list(range(OBJECT_COUNT)) | ||
yoney marked this conversation as resolved.
Show resolved
Hide resolved
|
||
shuffle(heap) | ||
heapq.heapify(heap) | ||
|
||
# Each thread pops (OBJECT_COUNT / NTHREADS) items | ||
self.assertEqual(0, OBJECT_COUNT % NTHREADS) | ||
per_thread_pop_count = OBJECT_COUNT // NTHREADS | ||
|
||
def heappop_func(heap: list[int], pop_count: int): | ||
local_list = [] | ||
for _ in range(pop_count): | ||
item = heapq.heappop(heap) | ||
local_list.append(item) | ||
|
||
# Each local list should be sorted | ||
self.assertTrue(self.is_sorted_ascending(local_list)) | ||
|
||
self.run_concurrently( | ||
worker_func=heappop_func, | ||
args=(heap, per_thread_pop_count), | ||
nthreads=NTHREADS, | ||
) | ||
self.assertEqual(0, len(heap)) | ||
|
||
def test_racing_heappushpop(self): | ||
heap = list(range(OBJECT_COUNT)) | ||
shuffle(heap) | ||
heapq.heapify(heap) | ||
|
||
pushpop_items = [ | ||
randint(-OBJECT_COUNT, OBJECT_COUNT) for _ in range(OBJECT_COUNT) | ||
] | ||
|
||
def heappushpop_func(heap: list[int], pushpop_items: list[int]): | ||
for item in pushpop_items: | ||
popped_item = heapq.heappushpop(heap, item) | ||
self.assertTrue(popped_item <= item) | ||
|
||
self.run_concurrently( | ||
worker_func=heappushpop_func, | ||
args=(heap, pushpop_items), | ||
nthreads=NTHREADS, | ||
) | ||
self.assertEqual(OBJECT_COUNT, len(heap)) | ||
self.assertTrue(self.is_min_heap_property_satisfied(heap)) | ||
|
||
def test_racing_heapreplace(self): | ||
heap = list(range(OBJECT_COUNT)) | ||
shuffle(heap) | ||
heapq.heapify(heap) | ||
|
||
replace_items = [ | ||
randint(-OBJECT_COUNT, OBJECT_COUNT) for _ in range(OBJECT_COUNT) | ||
] | ||
|
||
def heapreplace_func(heap: list[int], replace_items: list[int]): | ||
for item in replace_items: | ||
popped_item = heapq.heapreplace(heap, item) | ||
yoney marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
self.run_concurrently( | ||
worker_func=heapreplace_func, | ||
args=(heap, replace_items), | ||
nthreads=NTHREADS, | ||
) | ||
self.assertEqual(OBJECT_COUNT, len(heap)) | ||
self.assertTrue(self.is_min_heap_property_satisfied(heap)) | ||
|
||
def test_racing_heapify_max(self): | ||
max_heap = list(range(OBJECT_COUNT)) | ||
shuffle(max_heap) | ||
|
||
def heapify_max_func(max_heap: list[int]): | ||
heapq.heapify_max(max_heap) | ||
|
||
self.run_concurrently( | ||
worker_func=heapify_max_func, args=(max_heap,), nthreads=NTHREADS | ||
) | ||
self.assertTrue(self.is_max_heap_property_satisfied(max_heap)) | ||
|
||
def test_racing_heappush_max(self): | ||
max_heap = [] | ||
|
||
def heappush_max_func(max_heap: list[int]): | ||
for item in range(OBJECT_COUNT): | ||
heapq.heappush_max(max_heap, item) | ||
|
||
self.run_concurrently( | ||
worker_func=heappush_max_func, args=(max_heap,), nthreads=NTHREADS | ||
) | ||
self.assertTrue(self.is_max_heap_property_satisfied(max_heap)) | ||
|
||
def test_racing_heappop_max(self): | ||
max_heap = list(range(OBJECT_COUNT)) | ||
shuffle(max_heap) | ||
heapq.heapify_max(max_heap) | ||
|
||
# Each thread pops (OBJECT_COUNT / NTHREADS) items | ||
self.assertEqual(0, OBJECT_COUNT % NTHREADS) | ||
per_thread_pop_count = OBJECT_COUNT // NTHREADS | ||
|
||
def heappop_max_func(max_heap: list[int], pop_count: int): | ||
local_list = [] | ||
for _ in range(pop_count): | ||
item = heapq.heappop_max(max_heap) | ||
local_list.append(item) | ||
|
||
# Each local list should be sorted | ||
self.assertTrue(self.is_sorted_descending(local_list)) | ||
|
||
self.run_concurrently( | ||
worker_func=heappop_max_func, | ||
args=(max_heap, per_thread_pop_count), | ||
nthreads=NTHREADS, | ||
) | ||
self.assertEqual(0, len(max_heap)) | ||
yoney marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def test_racing_heappushpop_max(self): | ||
max_heap = list(range(OBJECT_COUNT)) | ||
shuffle(max_heap) | ||
heapq.heapify_max(max_heap) | ||
|
||
pushpop_items = [ | ||
randint(-OBJECT_COUNT, OBJECT_COUNT) for _ in range(OBJECT_COUNT) | ||
] | ||
|
||
def heappushpop_max_func( | ||
max_heap: list[int], pushpop_items: list[int] | ||
): | ||
for item in pushpop_items: | ||
popped_item = heapq.heappushpop_max(max_heap, item) | ||
self.assertTrue(popped_item >= item) | ||
|
||
self.run_concurrently( | ||
worker_func=heappushpop_max_func, | ||
args=(max_heap, pushpop_items), | ||
nthreads=NTHREADS, | ||
) | ||
self.assertEqual(OBJECT_COUNT, len(max_heap)) | ||
self.assertTrue(self.is_max_heap_property_satisfied(max_heap)) | ||
|
||
def test_racing_heapreplace_max(self): | ||
max_heap = list(range(OBJECT_COUNT)) | ||
shuffle(max_heap) | ||
heapq.heapify_max(max_heap) | ||
|
||
replace_items = [ | ||
randint(-OBJECT_COUNT, OBJECT_COUNT) for _ in range(OBJECT_COUNT) | ||
] | ||
|
||
def heapreplace_max_func( | ||
max_heap: list[int], replace_items: list[int] | ||
): | ||
for item in replace_items: | ||
popped_item = heapq.heapreplace_max(max_heap, item) | ||
yoney marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
self.run_concurrently( | ||
worker_func=heapreplace_max_func, | ||
args=(max_heap, replace_items), | ||
nthreads=NTHREADS, | ||
) | ||
self.assertEqual(OBJECT_COUNT, len(max_heap)) | ||
self.assertTrue(self.is_max_heap_property_satisfied(max_heap)) | ||
|
||
def is_min_heap_property_satisfied(self, heap: list[object]) -> bool: | ||
""" | ||
The value of a parent node should be less than or equal to the | ||
values of its children. | ||
""" | ||
return self.is_heap_property_satisfied(heap, HeapKind.MIN) | ||
|
||
def is_max_heap_property_satisfied(self, heap: list[object]) -> bool: | ||
""" | ||
The value of a parent node should be greater than or equal to the | ||
values of its children. | ||
""" | ||
return self.is_heap_property_satisfied(heap, HeapKind.MAX) | ||
|
||
@staticmethod | ||
def is_heap_property_satisfied( | ||
heap: list[object], heap_kind: HeapKind | ||
) -> bool: | ||
""" | ||
Check if the heap property is satisfied. | ||
""" | ||
op = operator.le if heap_kind == HeapKind.MIN else operator.ge | ||
# position 0 has no parent | ||
for pos in range(1, len(heap)): | ||
parent_pos = (pos - 1) >> 1 | ||
if not op(heap[parent_pos], heap[pos]): | ||
return False | ||
|
||
return True | ||
|
||
@staticmethod | ||
def is_sorted_ascending(lst: list[object]) -> bool: | ||
""" | ||
Check if the list is sorted in ascending order (non-decreasing). | ||
""" | ||
return all(lst[i - 1] <= lst[i] for i in range(1, len(lst))) | ||
|
||
@staticmethod | ||
def is_sorted_descending(lst: list[object]) -> bool: | ||
""" | ||
Check if the list is sorted in descending order (non-increasing). | ||
""" | ||
return all(lst[i - 1] >= lst[i] for i in range(1, len(lst))) | ||
yoney marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
@staticmethod | ||
def run_concurrently(worker_func, args, nthreads) -> None: | ||
""" | ||
Run the worker function concurrently in multiple threads. | ||
""" | ||
barrier = Barrier(NTHREADS) | ||
|
||
def wrapper_func(*args): | ||
# Wait for all threadss to reach this point before proceeding. | ||
yoney marked this conversation as resolved.
Show resolved
Hide resolved
|
||
barrier.wait() | ||
worker_func(*args) | ||
|
||
workers = [] | ||
for _ in range(nthreads): | ||
worker = Thread(target=wrapper_func, args=args) | ||
workers.append(worker) | ||
worker.start() | ||
|
||
yoney marked this conversation as resolved.
Show resolved
Hide resolved
|
||
for worker in workers: | ||
worker.join() | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
1 change: 1 addition & 0 deletions
1
Misc/NEWS.d/next/Core_and_Builtins/2025-06-02-13-57-40.gh-issue-116738.ycJsL8.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Make methods on :class:`heapq` thread-safe when the GIL is disabled. | ||
yoney marked this conversation as resolved.
Show resolved
Hide resolved
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.