Add retry logic to safe_gather utility

This commit is contained in:
Rafael Moraes
2025-10-21 14:51:08 -03:00
parent c9a5ff4a0e
commit 8268447357
+10 -1
View File
@@ -49,12 +49,21 @@ async def async_subprocess(*args: str, silent: bool = False) -> None:
async def safe_gather(
*tasks: typing.Awaitable[typing.Any],
limit: int = 5,
retries: int = 3,
) -> list[typing.Any]:
semaphore = asyncio.Semaphore(limit)
async def bounded_task(task: typing.Awaitable[typing.Any]) -> typing.Any:
async with semaphore:
return await task
last_exception = None
for attempt in range(retries + 1):
try:
return await task
except Exception as e:
last_exception = e
if attempt < retries:
await asyncio.sleep(2**attempt)
return last_exception
return await asyncio.gather(
*(bounded_task(task) for task in tasks),