Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a periodic timer #78

Closed
leandro-lucarella-frequenz opened this issue Mar 7, 2023 · 0 comments · Fixed by #79
Closed

Add a periodic timer #78

leandro-lucarella-frequenz opened this issue Mar 7, 2023 · 0 comments · Fixed by #79
Assignees
Labels
part:core Affects the core types (`Sender`, `Receiver`, exceptions, etc.) priority:high Address this as soon as possible type:enhancement New feature or enhancement visitble to users
Milestone

Comments

@leandro-lucarella-frequenz
Copy link
Contributor

What's needed?

The current timer fires interval time after the ready() method was called, but this is not suitable for periodic timers as it will accumulate timer drift over time.

We need a timer that will periodically fire every interval time.

Proposed solution

Extend Timer to have a periodic option or create a new PeriodicTimer that will start when the timer is instantiated and will fire periodically every interval time, no matter if ready() was called or not.

If the timer fired several times before ready()/consume() was called, those fires should be queued and returned eventually.

Use cases

The resampler needs this.

Alternatives and workarounds

One could use the current Timer and account for the time drifting oneself. But for that we need to create a new timer for each period, which is wasteful in terms of resources (allocations) and inconvenient in terms of programming effort.

Additional context

@leandro-lucarella-frequenz leandro-lucarella-frequenz added part:❓ We need to figure out which part is affected priority:❓ We need to figure out how soon this should be addressed type:enhancement New feature or enhancement visitble to users labels Mar 7, 2023
@leandro-lucarella-frequenz leandro-lucarella-frequenz added this to the v0.14.0 milestone Mar 7, 2023
@leandro-lucarella-frequenz leandro-lucarella-frequenz added priority:high Address this as soon as possible part:core Affects the core types (`Sender`, `Receiver`, exceptions, etc.) and removed part:❓ We need to figure out which part is affected priority:❓ We need to figure out how soon this should be addressed labels Mar 7, 2023
@leandro-lucarella-frequenz leandro-lucarella-frequenz moved this from To do to In progress in Python SDK Roadmap Mar 15, 2023
leandro-lucarella-frequenz pushed a commit that referenced this issue May 9, 2023
The current timer provided is very convenient to implement timers, but
it's not very helpful to implement a peridic timer that doesn't
accumulate drift.

This PR makes the timer configurable via *missing ticks policies* and
adds 3 policies: `TriggerAllMissing`, `SkipMissingAndResync` and
`SkipMissingAndDrift`.

For convenience, 2 alternative constructors are provided for the most
common cases: `Timer.timeout()` and `Timer.periodic()`. Timeout timers
always drift and periodic timers never.

The new `Timer` also uses the `asyncio` loop monotonic clock, and
returns the `drift` (difference between when the timer should have
triggered and it actually did) instead of a `datetime` with the time of
trigger.

It is also now possible to control when this timer is started, and can
be stopped and reset/restarted at will.

Fixes #78.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
part:core Affects the core types (`Sender`, `Receiver`, exceptions, etc.) priority:high Address this as soon as possible type:enhancement New feature or enhancement visitble to users
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant