Make Timer.interval fully adjustable at runtime#82
Open
wbarnha wants to merge 1 commit into
Open
Conversation
tick() and adjust_interval() already read self.interval_s fresh every iteration, but the drift-correction bounds (min_interval_s / max_interval_s) and the default max_drift threshold were derived from the interval once, in __init__. Assigning a new interval mid-run therefore left adjust_interval() clamping to the old cadence. Promote interval to a property whose setter recomputes interval_s, the default max_drift, and the correction bounds via a shared _configure_interval() helper (also used by __init__). Assigning timer.interval = X now retunes the timer live; the next tick uses the new cadence. No behaviour change for existing callers -- construction goes through the same path and yields identical values. Add tests: the setter recomputes all derived values (including the small-interval branch where the bounds collapse), and a runtime interval change is honoured by the iterator (the next sleep clamps to the new max_interval_s, not the old one). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Lt14zRDjGJv9YU9QZC3PqY
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Timer.tick()andTimer.adjust_interval()already readself.interval_sfresh on every iteration, so the nominal interval was effectively live. But
the values derived from the interval — the drift-correction bounds
min_interval_s/max_interval_sand the defaultmax_driftthreshold —were computed once, in
__init__. Reassigning the interval after the timerstarted therefore left
adjust_interval()clamping sleep times to the oldcadence, so the interval was not actually adjustable at runtime.
This promotes
intervalto a property whose setter recomputes allinterval-derived state through a shared
_configure_interval()helper (alsoused by
__init__). Assigningtimer.interval = Xnow retunes the timerlive — the next
tick()uses the new interval, bounds, and default threshold.Behavior
_configure_interval()path and produces identical values.timer.interval = Xat runtime now updatesinterval_s,min_interval_s,max_interval_s, and the (interval-derived) defaultmax_drift.max_driftis interval-derived, changing the intervalrefreshes it; if you have pinned a custom
max_drift, set it after changingthe interval.
Tests
test_Timer_interval__setter_recomputes_derived_values— the setterrefreshes
interval_s,max_drift, and both correction bounds.test_Timer_interval__small_interval_collapses_bounds— thesub-correction-window branch still collapses the bounds onto the interval.
test_Timer_interval__can_be_changed_at_runtime— a live interval change ishonored by the iterator: the next sleep clamps to the new
max_interval_s,not the old one (so the assertion only holds if the change took effect).
Full suite: 755 passed, 1 skipped locally (Python 3.11);
ruff checkandruff format --checkclean.Context
Follow-up to the drift-warning work in #78 (which makes
max_driftconfigurable) and issue #46 — this extends the same "retune a running timer"
idea to the interval itself. It is independent of #78; if both land there is a
trivial overlap in
Timer.__init__.🤖 Generated with Claude Code
https://claude.ai/code/session_01Lt14zRDjGJv9YU9QZC3PqY
Generated by Claude Code