Skip to content

Commit 8bb445e

Browse files
authored
timing_tools.py: Decrease Logging More (#114)
1 parent 012a263 commit 8bb445e

File tree

1 file changed

+31
-12
lines changed

1 file changed

+31
-12
lines changed

wipac_dev_tools/timing_tools.py

+31-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Utilities for timers, interval trackers, etc."""
22

33
import asyncio
4+
import itertools
45
import logging
56
import time
67
from typing import Union
@@ -29,50 +30,68 @@ def __init__(
2930
self.logger = logging.getLogger(logger)
3031

3132
def fastforward(self):
32-
"""Reset the timer so that the next call to `has_interval_elapsed` will return True.
33+
"""Force the interval timer to consider the current interval as already elapsed.
3334
34-
This effectively skips the current interval and forces the timer to indicate
35-
that the interval has elapsed on the next check.
35+
This resets the internal timer to a state where the next call to `has_interval_elapsed`
36+
will immediately return `True`, as if the interval has already passed.
3637
"""
3738
self._last_time = float("-inf")
3839

39-
async def wait_until_interval(self, frequency: float = 1.0) -> None:
40+
@staticmethod
41+
def _is_nth(i: int, nth: int) -> bool:
42+
return nth > 0 and i % nth == 0
43+
44+
async def wait_until_interval(
45+
self,
46+
frequency: float = 1.0,
47+
log_every_nth: int = 60,
48+
) -> None:
4049
"""Wait asynchronously until the specified interval has elapsed.
4150
4251
This method checks the elapsed time every `frequency` seconds,
4352
allowing cooperative multitasking during the wait.
4453
"""
4554
if self.logger:
4655
self.logger.debug(
47-
f"Waiting until {self.seconds}s has elapsed since the last iteration..."
56+
f"Waiting for {self.seconds}s interval before proceeding..."
4857
)
49-
while not self.has_interval_elapsed():
58+
59+
for i in itertools.count():
60+
if self.has_interval_elapsed(do_log=self._is_nth(i, log_every_nth)):
61+
return
5062
await asyncio.sleep(frequency)
5163

52-
def wait_until_interval_sync(self, frequency: float = 1.0) -> None:
64+
def wait_until_interval_sync(
65+
self,
66+
frequency: float = 1.0,
67+
log_every_nth: int = 60,
68+
) -> None:
5369
"""Wait until the specified interval has elapsed.
5470
5571
This method checks the elapsed time every `frequency` seconds,
5672
blocking until the interval has elapsed.
5773
"""
5874
if self.logger:
5975
self.logger.debug(
60-
f"Waiting until {self.seconds}s has elapsed since the last iteration..."
76+
f"Waiting for {self.seconds}s interval before proceeding..."
6177
)
62-
while not self.has_interval_elapsed():
78+
79+
for i in itertools.count():
80+
if self.has_interval_elapsed(do_log=self._is_nth(i, log_every_nth)):
81+
return
6382
time.sleep(frequency)
6483

65-
def has_interval_elapsed(self) -> bool:
84+
def has_interval_elapsed(self, do_log: bool = True) -> bool:
6685
"""Check if the specified time interval has elapsed since the last expiration.
6786
6887
If the interval has elapsed, the internal timer is reset to the current time.
6988
"""
7089
diff = time.monotonic() - self._last_time
7190
if diff >= self.seconds:
7291
self._last_time = time.monotonic()
73-
if self.logger:
92+
if self.logger and do_log:
7493
self.logger.debug(
75-
f"At least {self.seconds}s have elapsed (actually {diff}s)."
94+
f"Interval elapsed: {self.seconds}s (actual: {diff:.3f}s)."
7695
)
7796
return True
7897
return False

0 commit comments

Comments
 (0)