callback `log_failed_attempt` added

This commit is contained in:
Daniil Fajnberg 2022-01-26 11:00:29 +01:00
parent d29ebeb7d6
commit bc212ef002
4 changed files with 12 additions and 1 deletions

View File

@ -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. Makes it more convenient to run awaitable objects in concurrent batches.
### log_failed_attempt
Prototypical `callback` argument for the `attempt` decorator.
## Decorators ## Decorators
### @in_async_session ### @in_async_session

View File

@ -1,6 +1,6 @@
[metadata] [metadata]
name = webutils-df name = webutils-df
version = 0.1.1 version = 0.1.2
author = Daniil F. author = Daniil F.
author_email = mail@placeholder123.to author_email = mail@placeholder123.to
description = Miscellaneous web utilities description = Miscellaneous web utilities

View File

@ -1,5 +1,6 @@
from .util import ( from .util import (
in_async_session, in_async_session,
attempt, attempt,
log_failed_attempt,
gather_in_batches gather_in_batches
) )

View File

@ -147,6 +147,12 @@ def attempt(_func: AsyncFunction = None, *,
return decorator if _func is None else decorator(_func) 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: 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. Simple extension of the `asyncio.gather` function to make it easy to run awaitable objects in concurrent batches.