Skip to content

Commit

Permalink
fix: start runners early to compensate for deferred scheduling
Browse files Browse the repository at this point in the history
  • Loading branch information
thegamecracks committed Jun 20, 2024
1 parent 6cb6006 commit ca917e9
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions sardine_core/scheduler/async_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,11 @@ def defer_beats(self) -> float:
"""The number of beats to defer function calls."""
return float(self.scheduler.deferred)

@property
def defer_duration(self) -> float:
"""The amount of time to defer function calls."""
return self.defer_beats * self.clock.beat_duration

@property
def env(self) -> "FishBowl":
"""A shorthand for the scheduler's fish bowl."""
Expand Down Expand Up @@ -676,10 +681,11 @@ async def _run_once(self) -> None:
arriving_states: list[DeferredState] = []
while self.deferred_states:
entry = self.deferred_states[0]
entry_deadline = entry.deadline - self.defer_duration
if (
self.clock.time >= entry.deadline
self.clock.time >= entry_deadline
or state is not None
and deadline >= entry.deadline
and deadline >= entry_deadline
):
heapq.heappop(self.deferred_states)

Expand Down Expand Up @@ -709,7 +715,7 @@ async def _run_once(self) -> None:
# sleeping a full period.
deadline = self.deferred_states[0].deadline
# interrupted is true if we are past the deadline
interrupted = await self._sleep_until(deadline)
interrupted = await self._sleep_until(deadline - self.defer_duration)
return self._jump_start_iteration()

# NOTE: deadline will always be defined at this point
Expand Down Expand Up @@ -745,7 +751,7 @@ async def _call_func(self, func, args, kwargs):

if self.defer_beats:
delta = self.clock.time - self._expected_time
shift = self.defer_beats * self.clock.beat_duration - delta
shift = self.defer_duration - delta
self.time.shift += shift

return await maybe_coro(func, *args, **valid_kwargs)
Expand Down

0 comments on commit ca917e9

Please sign in to comment.