-
Notifications
You must be signed in to change notification settings - Fork 9
An optional tick_at_start parameter has been added to Timer
#372
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -477,6 +477,7 @@ def __init__( # noqa: DOC503 pylint: disable=too-many-arguments | |
| *, | ||
| auto_start: bool = True, | ||
| start_delay: timedelta = timedelta(0), | ||
| tick_at_start: bool = False, | ||
| loop: asyncio.AbstractEventLoop | None = None, | ||
| ) -> None: | ||
| """Initialize this timer. | ||
|
|
@@ -497,6 +498,13 @@ def __init__( # noqa: DOC503 pylint: disable=too-many-arguments | |
| start_delay: The delay before the timer should start. If `auto_start` is | ||
| `False`, an exception is raised. This has microseconds resolution, | ||
| anything smaller than a microsecond means no delay. | ||
| tick_at_start: When `True`, the timer will trigger immediately after | ||
| starting, and then wait for the interval before triggering | ||
| again. When `False`, the timer will wait the interval before | ||
| triggering for the first time. If `auto_start` is `False` and | ||
| this is `True`, an exception is raised. If a `start_delay` is | ||
| specified and this is `True`, the first trigger will be immediately | ||
| after the `start_delay`. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One use-case we have is that you want to trigger immediately, then wait for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder whether it should be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
No, I remember that use case and this should reduce its complexity and make it readable. But I feel that this is the right balance, and But we can come back to it. And if we are going to add more There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What I meant is replace There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we have only There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Which case do you mean here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Discussed in person, we take it in for now and keep potential changes for v2.0 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've added an item describing the 2.0 plan here: #365 |
||
| loop: The event loop to use to track time. If `None`, | ||
| `asyncio.get_running_loop()` will be used. | ||
|
|
||
|
|
@@ -517,6 +525,9 @@ def __init__( # noqa: DOC503 pylint: disable=too-many-arguments | |
| "`auto_start` must be `True` if a `start_delay` is specified" | ||
| ) | ||
|
|
||
| if tick_at_start is True and auto_start is False: | ||
| raise ValueError("`auto_start` must be `True` if `tick_at_start` is `True`") | ||
|
|
||
| self._interval: int = _to_microseconds(interval) | ||
| """The time to between timer ticks.""" | ||
|
|
||
|
|
@@ -567,7 +578,7 @@ def __init__( # noqa: DOC503 pylint: disable=too-many-arguments | |
| """ | ||
|
|
||
| if auto_start: | ||
| self.reset(start_delay=start_delay) | ||
| self.reset(start_delay=start_delay, tick_at_start=tick_at_start) | ||
|
|
||
| @property | ||
| def interval(self) -> timedelta: | ||
|
|
@@ -595,6 +606,7 @@ def reset( # noqa: DOC503 | |
| *, | ||
| interval: timedelta | None = None, | ||
| start_delay: timedelta = timedelta(0), | ||
| tick_at_start: bool = False, | ||
| ) -> None: | ||
| """Reset the timer to start timing from now (plus an optional delay). | ||
|
|
||
|
|
@@ -608,6 +620,12 @@ def reset( # noqa: DOC503 | |
| interval is kept. | ||
| start_delay: The delay before the timer should start. This has microseconds | ||
| resolution, anything smaller than a microsecond means no delay. | ||
| tick_at_start: When `True`, the timer will trigger immediately after | ||
| starting, and then wait for the interval before triggering | ||
| again. When `False`, the timer will wait the interval before | ||
| triggering for the first time. If a `start_delay` is specified | ||
| and this is `True`, the first trigger will be immediately after | ||
| the `start_delay`. | ||
|
|
||
| Raises: | ||
| RuntimeError: If it was called without a running loop. | ||
|
|
@@ -621,7 +639,10 @@ def reset( # noqa: DOC503 | |
| if interval is not None: | ||
| self._interval = _to_microseconds(interval) | ||
|
|
||
| self._next_tick_time = self._now() + start_delay_ms + self._interval | ||
| self._next_tick_time = self._now() + start_delay_ms | ||
|
|
||
| if not tick_at_start: | ||
| self._next_tick_time += self._interval | ||
|
|
||
| if self.is_running: | ||
| self._reset_event.set() | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't get why raising if
auto_startisFalse? What is the issue with making this first tick delayed to when the timer actually starts, like when usingstart_delay?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Restricted by how it is implemented. But if they need it, users can specify
tick_on_start=Truein the call toreset, just like withstart_delay.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, that's not how
start_delayis implemented. It also doesn't work withauto_start=False.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mmm, OK.