Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/asynchrony/various.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import time
import asyncio


async def retry_with_backoff(func, max_retries=3):
if max_retries < 1:
raise ValueError("max_retries must be at least 1")
last_exception = None
# Precompute backoff values outside the loop for faster lookups
backoffs = [0.0001 * attempt for attempt in range(max_retries)]
for attempt in range(max_retries):
try:
return await func()
except Exception as e:
last_exception = e
if attempt < max_retries - 1:
time.sleep(0.0001 * attempt)
# Use asyncio.sleep instead of time.sleep for non-blocking behavior in async context
# This avoids blocking the event loop, allowing concurrency and better performance
await asyncio.sleep(backoffs[attempt])
raise last_exception