Skip to content

Commit d562837

Browse files
committed
kafka.net.selector: break scheduled heapq ties when tasks have same scheduled_at
1 parent ab1f9ca commit d562837

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

kafka/net/selector.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ def __init__(self, coro):
5555
self._exc = None
5656
self.scheduled_at = None
5757

58+
def __lt__(self, other):
59+
# heapq requires the heap entries to be orderable. When two tasks
60+
# share the same scheduled_at, we don't care which fires first --
61+
# id() gives us a stable, unique-per-live-object tiebreaker.
62+
return id(self) < id(other)
63+
5864
def __call__(self, arg=None):
5965
ret, exc = (None, arg) if isinstance(arg, Exception) else (arg, None)
6066
while True:
@@ -151,7 +157,7 @@ def __init__(self, **configs):
151157
self._exception = None
152158
self._stop = False
153159
self._selector = self.config['selector']()
154-
self._scheduled = [] # managed by heapq
160+
self._scheduled = [] # managed by heapq; Task.__lt__ tiebreaks ties on scheduled_at
155161
self._ready = collections.deque()
156162
# Strong refs to every Task that hasn't completed yet. Without this,
157163
# a Task suspended on an externally-unreachable awaitable (e.g. a

0 commit comments

Comments
 (0)