Skip to content

Commit 05d51c4

Browse files
authored
Fix collision bug in ConcurrentHeap (#121)
1 parent 32014e7 commit 05d51c4

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

getm/concurrent/collections.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ def __init__(self, executor: Executor, concurrency: int=cpu_count()):
112112
super().__init__(executor, concurrency)
113113
self._futures: Set[Future] = set()
114114
self._scheduled: List[Any] = list()
115+
self._item_id = 0
115116

116117
def __len__(self):
117118
return len(self._scheduled) + len(self._futures)
@@ -123,7 +124,10 @@ def _submit(self):
123124

124125
def priority_put(self, priority: int, func: Callable, *args, **kwargs):
125126
# heapq implements a min queue. Negate the priority so heapq.heappop produces the expected ordering
126-
heapq.heappush(self._scheduled, (priority * -1, func, args, kwargs))
127+
# see priority queue docs: https://docs.python.org/3/library/heapq.html#priority-queue-implementation-notes
128+
q_priority = (-1 * priority, self._item_id)
129+
self._item_id += 1
130+
heapq.heappush(self._scheduled, (q_priority, func, args, kwargs))
127131
self._submit()
128132

129133
def put(self, func: Callable, *args, **kwargs):

0 commit comments

Comments
 (0)