Skip to content
Open
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
11 changes: 11 additions & 0 deletions src/asynchrony/various.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import time
import asyncio


async def retry_with_backoff(func, max_retries=3):
Expand All @@ -13,3 +14,13 @@ async def retry_with_backoff(func, max_retries=3):
if attempt < max_retries - 1:
time.sleep(0.0001 * attempt)
raise last_exception


async def fetch_user(user_id: int) -> dict:
"""Simulates fetching a user from a database"""
await asyncio.sleep(0.0001)
return {"id": user_id, "name": f"User{user_id}"}


async def fetch_all_users(user_ids: list[int]) -> list[dict]:
return await asyncio.gather(*[fetch_user(user_id) for user_id in user_ids])