Browse Source

[tasks] Fix behavior when task overruns interval

In a scenario with `tasks.loop(seconds=5)`:

The task takes 30 seconds to run on the first two iterations, and then
is nearly instant for iterations afterward. The behavior should be
that the task runs at:

t = 0  (on time)
t = 30 (late, should've been at t = 5)
t = 60 (late, should've been at t = 10)
t = 60 (late, should've been at t = 15)
t = 60 (late, should've been at t = 20)
t = 60 (late, should've been at t = 25)
... 6 more iterations
t = 60 (on time)
t = 65 (on time)

In a scenario with a loop with explicit times set at UTC 1pm, 2pm, 
3pm, 4pm, and 5pm:

- The task takes 6 hour to run on the first iteration, and then is
  nearly instant for iterations afterward. Assuming the task is started
  at noon, the behavior should be that the task runs at `t = 0` and
  then at `t = 3600` 4 times ("catching up" on the missed iterations
  at 2pm, 3pm, 4pm, and 5pm).

- The task takes 30 days to run on the first iteration, and then is
  nearly instant for iterations afterward. Assuming the task is started
  at noon, the behavior should be that the task runs at `t = 0` and
  then at `t = 43200` 149 times ("catching up" on the missed
  iterations for the past month).

This behavior should be documented in the ext.tasks docs
pull/7494/head
Sebastian Law 3 years ago
committed by GitHub
parent
commit
f1ac25809c
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 8
      discord/ext/tasks/__init__.py

8
discord/ext/tasks/__init__.py

@ -175,16 +175,10 @@ class Loop(Generic[LF]):
raise
await asyncio.sleep(backoff.delay())
else:
await self._try_sleep_until(self._next_iteration)
if self._stop_next_iteration:
return
now = datetime.datetime.now(datetime.timezone.utc)
if now > self._next_iteration:
self._next_iteration = now
if self._time is not MISSING:
self._prepare_time_index(now)
await self._try_sleep_until(self._next_iteration)
self._current_loop += 1
if self._current_loop == self.count:

Loading…
Cancel
Save