Skip to content

Commit 4f928da

Browse files
Sync asyncio-walkthrough with the maintenance update of Python's asyncio: A Hands-On Walkthrough (#868)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 0fedeb7 commit 4f928da

9 files changed

Lines changed: 108 additions & 308 deletions

File tree

‎asyncio-walkthrough/areq.py‎

Lines changed: 0 additions & 123 deletions
This file was deleted.

‎asyncio-walkthrough/asyncq.py‎

Lines changed: 0 additions & 70 deletions
This file was deleted.

‎asyncio-walkthrough/chained.py‎

Lines changed: 30 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,45 @@
1-
#!/usr/env/bin python3
2-
# chained.py
3-
41
import asyncio
52
import random
63
import time
74

85

9-
async def randint(a: int, b: int) -> int:
10-
return random.randint(a, b)
11-
6+
async def main():
7+
user_ids = [1, 2, 3]
8+
start = time.perf_counter()
9+
await asyncio.gather(
10+
*(get_user_with_posts(user_id) for user_id in user_ids)
11+
)
12+
end = time.perf_counter()
13+
print(f"\n==> Total time: {end - start:.2f} seconds")
1214

13-
async def part1(n: int) -> str:
14-
i = await randint(0, 10)
15-
print(f"part1({n}) sleeping for {i} seconds.")
16-
await asyncio.sleep(i)
17-
result = f"result{n}-1"
18-
print(f"Returning part1({n}) == {result}.")
19-
return result
2015

16+
async def get_user_with_posts(user_id):
17+
user = await fetch_user(user_id)
18+
await fetch_posts(user)
2119

22-
async def part2(n: int, arg: str) -> str:
23-
i = await randint(0, 10)
24-
print(f"part2{n, arg} sleeping for {i} seconds.")
25-
await asyncio.sleep(i)
26-
result = f"result{n}-2 derived from {arg}"
27-
print(f"Returning part2{n, arg} == {result}.")
28-
return result
2920

21+
async def fetch_user(user_id):
22+
delay = random.uniform(0.5, 2.0)
23+
print(f"User coro: fetching user by {user_id=}...")
24+
await asyncio.sleep(delay)
25+
user = {"id": user_id, "name": f"User{user_id}"}
26+
print(f"User coro: fetched user with {user_id=} (done in {delay:.1f}s).")
27+
return user
3028

31-
async def chain(n: int) -> None:
32-
start = time.perf_counter()
33-
p1 = await part1(n)
34-
p2 = await part2(n, p1)
35-
end = time.perf_counter() - start
36-
print(f"-->Chained result{n} => {p2} (took {end:0.2f} seconds).")
3729

38-
39-
async def main(*args):
40-
await asyncio.gather(*(chain(n) for n in args))
30+
async def fetch_posts(user):
31+
delay = random.uniform(0.5, 2.0)
32+
print(f"Post coro: retrieving posts for {user['name']}...")
33+
await asyncio.sleep(delay)
34+
posts = [f"Post {i} by {user['name']}" for i in range(1, 3)]
35+
print(
36+
f"Post coro: got {len(posts)} posts by {user['name']}"
37+
f" (done in {delay:.1f}s):"
38+
)
39+
for post in posts:
40+
print(f" - {post}")
4141

4242

4343
if __name__ == "__main__":
44-
import sys
45-
4644
random.seed(444)
47-
args = [1, 2, 3] if len(sys.argv) == 1 else map(int, sys.argv[1:])
48-
start = time.perf_counter()
49-
asyncio.run(main(*args))
50-
end = time.perf_counter() - start
51-
print(f"Program finished in {end:0.2f} seconds.")
45+
asyncio.run(main())

‎asyncio-walkthrough/countasync.py‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
#!/usr/bin/env python3
2-
# countasync.py
3-
41
import asyncio
52

63

74
async def count():
85
print("One")
96
await asyncio.sleep(1)
107
print("Two")
8+
await asyncio.sleep(1)
119

1210

1311
async def main():
@@ -17,7 +15,7 @@ async def main():
1715
if __name__ == "__main__":
1816
import time
1917

20-
s = time.perf_counter()
18+
start = time.perf_counter()
2119
asyncio.run(main())
22-
elapsed = time.perf_counter() - s
20+
elapsed = time.perf_counter() - start
2321
print(f"{__file__} executed in {elapsed:0.2f} seconds.")

‎asyncio-walkthrough/countsync.py‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
#!/usr/bin/env python3
2-
# countsync.py
3-
41
import time
52

63

74
def count():
85
print("One")
96
time.sleep(1)
107
print("Two")
8+
time.sleep(1)
119

1210

1311
def main():
@@ -16,7 +14,7 @@ def main():
1614

1715

1816
if __name__ == "__main__":
19-
s = time.perf_counter()
17+
start = time.perf_counter()
2018
main()
21-
elapsed = time.perf_counter() - s
19+
elapsed = time.perf_counter() - start
2220
print(f"{__file__} executed in {elapsed:0.2f} seconds.")

‎asyncio-walkthrough/phases.py‎

Lines changed: 0 additions & 35 deletions
This file was deleted.

0 commit comments

Comments
 (0)