Skip to content

Commit 9692e75

Browse files
committed
add another async example
1 parent c9a4d68 commit 9692e75

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/asynchrony/various.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import time
2+
import asyncio
23

34

45
async def retry_with_backoff(func, max_retries=3):
@@ -13,3 +14,17 @@ async def retry_with_backoff(func, max_retries=3):
1314
if attempt < max_retries - 1:
1415
time.sleep(0.0001 * attempt)
1516
raise last_exception
17+
18+
19+
async def fetch_user(user_id: int) -> dict:
20+
"""Simulates fetching a user from a database"""
21+
await asyncio.sleep(0.0001)
22+
return {"id": user_id, "name": f"User{user_id}"}
23+
24+
25+
async def fetch_all_users(user_ids: list[int]) -> list[dict]:
26+
users = []
27+
for user_id in user_ids:
28+
user = await fetch_user(user_id)
29+
users.append(user)
30+
return users

0 commit comments

Comments
 (0)