From bc212ef0028283ca40e2fc9041bd61d3ce733f91 Mon Sep 17 00:00:00 2001 From: Daniil Fajnberg Date: Wed, 26 Jan 2022 11:00:29 +0100 Subject: [PATCH] callback `log_failed_attempt` added --- README.md | 4 ++++ setup.cfg | 2 +- src/webutils_df/__init__.py | 1 + src/webutils_df/util.py | 6 ++++++ 4 files changed, 12 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d0c9e12..daa26f4 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,10 @@ Stuff I frequently use in various unrelated projects that deal with web requests Makes it more convenient to run awaitable objects in concurrent batches. +### log_failed_attempt + +Prototypical `callback` argument for the `attempt` decorator. + ## Decorators ### @in_async_session diff --git a/setup.cfg b/setup.cfg index c642716..6a32c24 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = webutils-df -version = 0.1.1 +version = 0.1.2 author = Daniil F. author_email = mail@placeholder123.to description = Miscellaneous web utilities diff --git a/src/webutils_df/__init__.py b/src/webutils_df/__init__.py index 5711caf..1654dbf 100644 --- a/src/webutils_df/__init__.py +++ b/src/webutils_df/__init__.py @@ -1,5 +1,6 @@ from .util import ( in_async_session, attempt, + log_failed_attempt, gather_in_batches ) diff --git a/src/webutils_df/util.py b/src/webutils_df/util.py index 7ed5def..11bacac 100644 --- a/src/webutils_df/util.py +++ b/src/webutils_df/util.py @@ -147,6 +147,12 @@ def attempt(_func: AsyncFunction = None, *, return decorator if _func is None else decorator(_func) +async def log_failed_attempt(f: Callable, e: Exception, n: int, delay: float, args: tuple, kwargs: dict) -> None: + """Intended to be the prototypical `callback` argument for the `attempt` decorator above.""" + arg_str = f"{', '.join(repr(arg) for arg in args)}, {', '.join(f'{k}={repr(v)}' for k, v in kwargs.items())}" + logger.warning(f"Attempt {n} at {f.__name__}({arg_str}) failed with {repr(e)}; retrying after {delay} seconds.") + + async def gather_in_batches(batch_size: int, *aws: Awaitable, return_exceptions: bool = False) -> list: """ Simple extension of the `asyncio.gather` function to make it easy to run awaitable objects in concurrent batches.