From 6f231806bf034a1b9b2981e09e43e85282c3ab5f Mon Sep 17 00:00:00 2001 From: BobTheBuidler <70677534+BobTheBuidler@users.noreply.github.com> Date: Mon, 16 Dec 2024 13:25:32 -0400 Subject: [PATCH] feat: re-enable smart processing queue heap sorting but with less cpu usage than before --- a_sync/primitives/queue.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/a_sync/primitives/queue.py b/a_sync/primitives/queue.py index 72a538e7..52b79d70 100644 --- a/a_sync/primitives/queue.py +++ b/a_sync/primitives/queue.py @@ -727,10 +727,11 @@ def _get(self, heappop=heapq.heappop): >>> task = queue._get() >>> print(task) """ - # NOTE: Since waiter priorities can change, this might not return the job with the - # most waiters if `self._queue` is not currently in order, but after calling `heappop`, - # `self._queue` will always be in proper order for next call to `self._get`. - return heappop(self._queue) + # NOTE: Since waiter priorities can change, heappop might not return the job with the + # most waiters if `self._queue` is not currently in order, but we can use `heappushpop`, + # to ensure we get the job with the most waiters. + queue = self._queue + return heappushpop(queue, heappop(queue)) def _get_key(self, *args, **kwargs) -> _SmartKey: """