Skip to content

Commit

Permalink
Clean up Timer tasks
Browse files Browse the repository at this point in the history
Signed-off-by: Mathias L. Baumann <mathias.baumann@frequenz.com>
  • Loading branch information
Marenz committed Sep 23, 2024
1 parent 68eb88e commit 5827ddf
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/frequenz/channels/timer.py
Original file line number Diff line number Diff line change
Expand Up @@ -682,14 +682,17 @@ async def ready(self) -> bool: # noqa: DOC502
# could be reset while we are sleeping, in which case we need to recalculate
# the time to the next tick and try again.
while time_to_next_tick > 0:
await next(
asyncio.as_completed(
[
asyncio.sleep(time_to_next_tick / 1_000_000),
self._reset_event.wait(),
]
)
)
tasks = [
asyncio.create_task(asyncio.sleep(time_to_next_tick / 1_000_000)),
asyncio.create_task(self._reset_event.wait()),
]

try:
await next(asyncio.as_completed(tasks))
finally:
tasks[0].cancel()
tasks[1].cancel()

self._reset_event.clear()
now = self._now()
time_to_next_tick = self._next_tick_time - now
Expand Down

0 comments on commit 5827ddf

Please sign in to comment.