Skip to content

Commit

Permalink
Clean up IntervalLoop
Browse files Browse the repository at this point in the history
  • Loading branch information
hypergonial committed Nov 19, 2023
1 parent 914622d commit 440645c
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion utils/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,29 @@
import inspect
import logging
import traceback
import typing as t


class IntervalLoop:
"""A simple interval loop that runs a coroutine at a specified interval.
Parameters
----------
callback : Callable[..., Awaitable[None]]
The coroutine to run at the specified interval.
seconds : float, optional
The number of seconds to wait before running the coroutine again.
minutes : float, optional
The number of minutes to wait before running the coroutine again.
hours : float, optional
The number of hours to wait before running the coroutine again.
days : float, optional
The number of days to wait before running the coroutine again.
"""

def __init__(
self,
callback,
callback: t.Callable[..., t.Awaitable[None]],
seconds: float | None = None,
minutes: float | None = None,
hours: float | None = None,
Expand All @@ -31,6 +48,8 @@ def __init__(
raise TypeError("Expected a coroutine function.")

async def _loopy_loop(self, *args, **kwargs) -> None:
"""Main loop logic"""

while not self._stop_next:
try:
await self._coro(*args, **kwargs)
Expand Down

0 comments on commit 440645c

Please sign in to comment.