-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
72 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .decorator_timer import repeat_every |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import asyncio | ||
import logging | ||
from asyncio import ensure_future | ||
from functools import wraps | ||
from traceback import format_exception | ||
from typing import Any, Callable, Coroutine, Optional, Union | ||
|
||
from starlette.concurrency import run_in_threadpool | ||
|
||
NoArgsNoReturnFuncT = Callable[[], None] | ||
NoArgsNoReturnAsyncFuncT = Callable[[], Coroutine[Any, Any, None]] | ||
NoArgsNoReturnDecorator = Callable[[Union[NoArgsNoReturnFuncT, NoArgsNoReturnAsyncFuncT]], NoArgsNoReturnAsyncFuncT] | ||
|
||
|
||
def repeat_every( | ||
*, | ||
seconds: float, | ||
wait_first: bool = False, | ||
logger: Optional[logging.Logger] = None, | ||
raise_exceptions: bool = False, | ||
max_repetitions: Optional[int] = None, | ||
) -> NoArgsNoReturnDecorator: | ||
|
||
def decorator(func: Union[NoArgsNoReturnAsyncFuncT, NoArgsNoReturnFuncT]) -> NoArgsNoReturnAsyncFuncT: | ||
""" | ||
Converts the decorated function into a repeated, periodically-called version of itself. | ||
""" | ||
is_coroutine = asyncio.iscoroutinefunction(func) | ||
|
||
@wraps(func) | ||
async def wrapped() -> None: | ||
repetitions = 0 | ||
|
||
async def loop() -> None: | ||
nonlocal repetitions | ||
if wait_first: | ||
await asyncio.sleep(seconds) | ||
while max_repetitions is None or repetitions < max_repetitions: | ||
try: | ||
if is_coroutine: | ||
await func() # type: ignore | ||
else: | ||
await run_in_threadpool(func) | ||
repetitions += 1 | ||
except Exception as exc: | ||
if logger is not None: | ||
formatted_exception = "".join(format_exception(type(exc), exc, exc.__traceback__)) | ||
logger.error(formatted_exception) | ||
if raise_exceptions: | ||
raise exc | ||
await asyncio.sleep(seconds) | ||
|
||
ensure_future(loop()) | ||
|
||
return wrapped | ||
|
||
return decorator |