Skip to content

Commit 82986fa

Browse files
committed
Timer: Allow setting a new interval in reset
Signed-off-by: Mathias L. Baumann <[email protected]>
1 parent 085fba0 commit 82986fa

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

RELEASE_NOTES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Frequenz channels Release Notes
22

3+
## New Features
4+
5+
- `Timer.reset()` now supports setting the interval and will restart the timer with the new interval.
6+
37
## Bug Fixes
48

59
- `FileWatcher`: Fixed `ready()` method to return False when an error occurs. Before this fix, `select()` (and other code using `ready()`) never detected the `FileWatcher` was stopped and the `select()` loop was continuously waking up to inform the receiver was ready.

src/frequenz/channels/timer.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,12 @@ def is_running(self) -> bool:
586586
"""Whether the timer is running."""
587587
return not self._stopped
588588

589-
def reset(self, *, start_delay: timedelta = timedelta(0)) -> None:
589+
def reset(
590+
self,
591+
*,
592+
interval: timedelta | None = None,
593+
start_delay: timedelta = timedelta(0),
594+
) -> None:
590595
"""Reset the timer to start timing from now (plus an optional delay).
591596
592597
If the timer was stopped, or not started yet, it will be started.
@@ -595,6 +600,8 @@ def reset(self, *, start_delay: timedelta = timedelta(0)) -> None:
595600
more details.
596601
597602
Args:
603+
interval: The new interval between ticks. If `None`, the current
604+
interval is kept.
598605
start_delay: The delay before the timer should start. This has microseconds
599606
resolution, anything smaller than a microsecond means no delay.
600607
@@ -607,6 +614,9 @@ def reset(self, *, start_delay: timedelta = timedelta(0)) -> None:
607614
if start_delay_ms < 0:
608615
raise ValueError(f"`start_delay` can't be negative, got {start_delay}")
609616

617+
if interval is not None:
618+
self._interval = _to_microseconds(interval)
619+
610620
self._next_tick_time = self._now() + start_delay_ms + self._interval
611621

612622
if self.is_running:

0 commit comments

Comments
 (0)