Optimize collector loop to account for actual execution time.

Previously, the loop did not consider the time spent on `collect_once`, potentially causing delays. By adjusting the sleep duration dynamically, the poll interval remains consistent as intended.
This commit is contained in:
2026-02-14 15:50:31 +01:00
parent d9dfde1c87
commit 1ad237d750

View File

@@ -271,8 +271,11 @@ async def collect_once() -> None:
async def collector_loop(stop_event: asyncio.Event) -> None: async def collector_loop(stop_event: asyncio.Event) -> None:
while not stop_event.is_set(): while not stop_event.is_set():
cycle_started = asyncio.get_running_loop().time()
await collect_once() await collect_once()
elapsed = asyncio.get_running_loop().time() - cycle_started
sleep_for = max(0.0, settings.poll_interval_seconds - elapsed)
try: try:
await asyncio.wait_for(stop_event.wait(), timeout=settings.poll_interval_seconds) await asyncio.wait_for(stop_event.wait(), timeout=sleep_for)
except asyncio.TimeoutError: except asyncio.TimeoutError:
pass pass