-
-
Notifications
You must be signed in to change notification settings - Fork 56
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
6 changed files
with
41 additions
and
15 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
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 |
---|---|---|
@@ -1,26 +1,40 @@ | ||
import asyncio | ||
from typing import Callable | ||
import logging | ||
from typing import Callable, Optional | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class AsyncTimer: | ||
def __init__(self, callback: Callable, timer_calc: Callable[[int], int]): | ||
self.callback = callback | ||
self.timer_calc = timer_calc | ||
self.timer = None | ||
self.tries = 0 | ||
self.timer: Optional[asyncio.Task] = None | ||
self.tries: int = 0 | ||
|
||
def reset(self): | ||
self.tries = 0 | ||
if self.timer: | ||
if self.timer and not self.timer.done(): | ||
self.timer.cancel() | ||
self.timer = None | ||
logger.debug( | ||
"AsyncTimer has been reset and any scheduler tasks have been cancelled" | ||
) | ||
|
||
def schedule_timeout(self): | ||
if self.timer: | ||
self.timer.cancel() | ||
|
||
self.timer = asyncio.create_task(self._run_timer()) | ||
|
||
async def _run_timer(self): | ||
await asyncio.sleep(self.timer_calc(self.tries + 1)) | ||
self.tries += 1 | ||
await self.callback() | ||
delay = self.timer_calc(self.tries + 1) | ||
logger.debug(f"Scheduling callback to run after {delay} seconds.") | ||
self.timer = asyncio.create_task(self._run_timer(delay)) | ||
|
||
async def _run_timer(self, delay: float): | ||
try: | ||
await asyncio.sleep(delay) | ||
await self.callback() | ||
except asyncio.CancelledError: | ||
logger.debug("AsyncTimer task was cancelled.") | ||
except Exception as e: | ||
logger.exception(f"Error in AsyncTimer callback: {e}") |