Skip to content

Commit

Permalink
Add a very simple ResultGroup.as_completed() helper.
Browse files Browse the repository at this point in the history
Refs #746
  • Loading branch information
coleifer committed Jun 10, 2023
1 parent 63bcf73 commit 4737bdf
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions huey/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import warnings

from collections import OrderedDict
from collections import deque
from functools import partial
from functools import wraps

Expand Down Expand Up @@ -1050,6 +1051,18 @@ def __iter__(self):
return iter(self._results)
def __len__(self):
return len(self._results)
def as_completed(self, backoff=1.15, max_delay=1.0):
res = deque(self._results)
delay = {r.id: 0. for r in res}
while res:
r = res.popleft()
if delay[r.id]:
time.sleep(delay[r.id])
if r._get() is EmptyData:
res.append(r)
delay[r.id] = min((delay[r.id] or 0.1) * backoff, max_delay)
else:
yield r.get()


dash_re = re.compile(r'(\d+)-(\d+)')
Expand Down

0 comments on commit 4737bdf

Please sign in to comment.