From ac9e0e922aaf7778f9d3dab3cc9c3c1d68f7d4d3 Mon Sep 17 00:00:00 2001 From: thegamecracks <61257169+thegamecracks@users.noreply.github.com> Date: Fri, 14 Jun 2024 17:05:15 -0400 Subject: [PATCH] refactor!: restore snap attribute on AsyncRunner AsyncRunner.snap describes an absolute deadline and serves a lower-level purpose than quant which describes an offset from the current phase. --- sardine_core/scheduler/async_runner.py | 40 +++++++++++++------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/sardine_core/scheduler/async_runner.py b/sardine_core/scheduler/async_runner.py index 0add1fde..712c6510 100644 --- a/sardine_core/scheduler/async_runner.py +++ b/sardine_core/scheduler/async_runner.py @@ -176,12 +176,12 @@ class AsyncRunner: running at different phases. To synchronize these functions together, their interval shifts should be set to the same value (usually 0). """ - quant: Optional[Union[float, int]] + snap: Optional[Union[float, int]] """ The absolute time that the next interval should start at. Setting this attribute will take priority over the regular interval - on the next iteration and cause the runner to wait until the quant + on the next iteration and cause the runner to wait until the snap deadline has arrived. The `delay_interval()` method combines this with interval shifting @@ -191,8 +191,8 @@ class AsyncRunner: this attribute will be reset to `None`. Note that deferred states will take priority over this, and in fact even - replace the quant, if one or more of those states specify a deadline earlier - than the current quant's deadline. + replace the snap, if one or more of those states specify a deadline earlier + than the current snap's deadline. """ _swimming: bool @@ -215,7 +215,7 @@ def __init__(self, name: str): self.states = deque(maxlen=self.MAX_FUNCTION_STATES) self.deferred_states = [] self.interval_shift = 0.0 - self.quant= None + self.snap = None self._iter = 0 self._default_period = 0.25 self.background_job = False @@ -314,10 +314,10 @@ def push(self, func: "MaybeCoroFunc", *args, **kwargs): It is recommended to reload the runner after this in case the current iteration sleeps past the deadline. - Note that this does not take priority over the `quant` attribute; - if quant is specified, the runner will continue to wait for that + Note that this does not take priority over the `snap` attribute; + if a snap is specified, the runner will continue to wait for that deadline to pass. If running a new function immediately is desired, - the `quant` should be set to `None` before reloading the runner. + the `snap` should be set to `None` before reloading the runner. Args: func (MaybeCoroFunc): The function to add. @@ -348,8 +348,8 @@ def push_deferred( It is recommended to reload the runner after this in case the current iteration sleeps past the deadline. - If there is an existing `quant` deadline, deferred states will take - priority and replace the `quant` attribute to ensure they run on time. + If there is an existing `snap` deadline, deferred states will take + priority and replace the `snap` attribute to ensure they run on time. Args: time (Union[float, int]): @@ -451,7 +451,7 @@ def allow_interval_correction(self): def delay_interval(self, deadline: Union[float, int], period: Union[float, int]): """Delays the next iteration until the given deadline has passed. - This is equivalent to setting the runner's `quant` attribute + This is equivalent to setting the runner's `snap` attribute to the deadline and also applying an appropriate interval shift to synchronize the period. @@ -469,12 +469,12 @@ def delay_interval(self, deadline: Union[float, int], period: Union[float, int]) Raises: RuntimeError: A function must be pushed before this can be used. """ - self.quant = deadline + self.snap = deadline self.interval_shift = self.clock.get_beat_time(period, time=deadline) - def _check_quant(self, time: float): - if self.quant is not None and time + self._last_interval >= self.quant: - self.quant = None + def _check_snap(self, time: float): + if self.snap is not None and time + self._last_interval >= self.snap: + self.snap = None def _correct_interval(self, period: Union[float, int]): """Checks if the interval should be corrected. @@ -517,8 +517,8 @@ def _get_next_deadline(self, period: Union[float, int]) -> float: The base interval is determined by the `period` argument, and then offsetted by the `interval_shift` attribute. - If the `quant` attribute is set to an absolute time - and the current clock time has not passed the quant, + If the `snap` attribute is set to an absolute time + and the current clock time has not passed the snap, it will take priority over whatever period was passed. Args: @@ -548,9 +548,9 @@ def _get_next_deadline(self, period: Union[float, int]) -> float: # the above solution could potentially trigger non-missed iterations too early. time = max(self._last_expected_time, min(self.clock.time, self._expected_time)) - self._check_quant(time) - if self.quant is not None: - return self.quant + self._check_snap(time) + if self.snap is not None: + return self.snap shifted_time = time + self.interval_shift