Skip to content

Commit 41a1a7e

Browse files
authored
Merge pull request #683 from realpython/python-asyncio
Sample code for the article on `asyncio`
2 parents 67525c7 + ce87219 commit 41a1a7e

File tree

12 files changed

+313
-0
lines changed

12 files changed

+313
-0
lines changed

python-asyncio/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Async I/O in Python: A Complete Walkthrough
2+
3+
This folder provides the code examples for the Real Python tutorial [Async I/O in Python: A Complete Walkthrough](https://realpython.com/async-io-python/).

python-asyncio/as_completed.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import asyncio
2+
import time
3+
4+
5+
async def coro(numbers):
6+
await asyncio.sleep(min(numbers))
7+
return list(reversed(numbers))
8+
9+
10+
async def main():
11+
task1 = asyncio.create_task(coro([10, 5, 2]))
12+
task2 = asyncio.create_task(coro([3, 2, 1]))
13+
print("Start:", time.strftime("%X"))
14+
for task in asyncio.as_completed([task1, task2]):
15+
result = await task
16+
print(f'result: {result} completed at {time.strftime("%X")}')
17+
print("End:", time.strftime("%X"))
18+
print(f"Both tasks done: {all((task1.done(), task2.done()))}")
19+
20+
21+
asyncio.run(main())

python-asyncio/chained.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import asyncio
2+
import random
3+
import time
4+
5+
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")
14+
15+
16+
async def get_user_with_posts(user_id):
17+
user = await fetch_user(user_id)
18+
await fetch_posts(user)
19+
20+
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
28+
29+
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}")
41+
42+
43+
if __name__ == "__main__":
44+
random.seed(444)
45+
asyncio.run(main())

python-asyncio/countasync.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import asyncio
2+
3+
4+
async def count():
5+
print("One")
6+
await asyncio.sleep(1)
7+
print("Two")
8+
await asyncio.sleep(1)
9+
10+
11+
async def main():
12+
await asyncio.gather(count(), count(), count())
13+
14+
15+
if __name__ == "__main__":
16+
import time
17+
18+
start = time.perf_counter()
19+
asyncio.run(main())
20+
elapsed = time.perf_counter() - start
21+
print(f"{__file__} executed in {elapsed:0.2f} seconds.")

python-asyncio/countsync.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import time
2+
3+
4+
def count():
5+
print("One")
6+
time.sleep(1)
7+
print("Two")
8+
time.sleep(1)
9+
10+
11+
def main():
12+
for _ in range(3):
13+
count()
14+
15+
16+
if __name__ == "__main__":
17+
start = time.perf_counter()
18+
main()
19+
elapsed = time.perf_counter() - start
20+
print(f"{__file__} executed in {elapsed:0.2f} seconds.")

python-asyncio/except_group.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import asyncio
2+
3+
4+
async def coro_a():
5+
await asyncio.sleep(1)
6+
raise ValueError("Error in coro A")
7+
8+
9+
async def coro_b():
10+
await asyncio.sleep(2)
11+
raise TypeError("Error in coro B")
12+
13+
14+
async def coro_c():
15+
await asyncio.sleep(0.5)
16+
raise IndexError("Error in coro C")
17+
18+
19+
async def main():
20+
results = await asyncio.gather(
21+
coro_a(), coro_b(), coro_c(), return_exceptions=True
22+
)
23+
exceptions = [e for e in results if isinstance(e, Exception)]
24+
if exceptions:
25+
raise ExceptionGroup("Errors", exceptions)
26+
27+
28+
try:
29+
asyncio.run(main())
30+
except* ValueError as ve_group:
31+
print(f"[ValueError handled] {ve_group.exceptions}")
32+
except* TypeError as te_group:
33+
print(f"[TypeError handled] {te_group.exceptions}")
34+
except* IndexError as ie_group:
35+
print(f"[IndexError handled] {ie_group.exceptions}")

python-asyncio/gathers.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import asyncio
2+
import time
3+
4+
5+
async def coro(numbers):
6+
await asyncio.sleep(min(numbers))
7+
return list(reversed(numbers))
8+
9+
10+
async def main():
11+
task1 = asyncio.create_task(coro([10, 5, 2]))
12+
task2 = asyncio.create_task(coro([3, 2, 1]))
13+
print("Start:", time.strftime("%X"))
14+
result = await asyncio.gather(task1, task2)
15+
print("End:", time.strftime("%X"))
16+
print(f"Both tasks done: {all((task1.done(), task2.done()))}")
17+
return result
18+
19+
20+
result = asyncio.run(main())
21+
print(f"result: {result}")

python-asyncio/powers.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import asyncio
2+
3+
4+
async def powers_of_two(stop=10):
5+
exponent = 0
6+
while exponent < stop:
7+
yield 2**exponent
8+
exponent += 1
9+
await asyncio.sleep(0.2) # Simulate some asynchronous work
10+
11+
12+
async def main():
13+
g = []
14+
async for i in powers_of_two(5):
15+
g.append(i)
16+
print(g)
17+
18+
f = [j async for j in powers_of_two(5) if not (j // 3 % 5)]
19+
print(f)
20+
21+
22+
if __name__ == "__main__":
23+
asyncio.run(main())

python-asyncio/queued.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import asyncio
2+
import random
3+
import time
4+
5+
6+
async def main():
7+
queue = asyncio.Queue()
8+
user_ids = [1, 2, 3]
9+
10+
start = time.perf_counter()
11+
await asyncio.gather(
12+
producer(queue, user_ids),
13+
*(consumer(queue) for _ in user_ids),
14+
)
15+
end = time.perf_counter()
16+
print(f"\n==> Total time: {end - start:.2f} seconds")
17+
18+
19+
async def producer(queue, user_ids):
20+
async def fetch_user(user_id):
21+
delay = random.uniform(0.5, 2.0)
22+
print(f"Producer: fetching user by {user_id=}...")
23+
await asyncio.sleep(delay)
24+
user = {"id": user_id, "name": f"User{user_id}"}
25+
print(f"Producer: fetched user with {user_id=} (done in {delay:.1f}s)")
26+
await queue.put(user)
27+
28+
await asyncio.gather(*(fetch_user(uid) for uid in user_ids))
29+
for _ in range(len(user_ids)):
30+
await queue.put(None) # Sentinels for consumers to terminate
31+
32+
33+
async def consumer(queue):
34+
while True:
35+
user = await queue.get()
36+
if user is None:
37+
break
38+
delay = random.uniform(0.5, 2.0)
39+
print(f"Consumer: retrieving posts for {user['name']}...")
40+
await asyncio.sleep(delay)
41+
posts = [f"Post {i} by {user['name']}" for i in range(1, 3)]
42+
print(
43+
f"Consumer: got {len(posts)} posts by {user['name']}"
44+
f" (done in {delay:.1f}s):"
45+
)
46+
for post in posts:
47+
print(f" - {post}")
48+
49+
50+
if __name__ == "__main__":
51+
random.seed(444)
52+
asyncio.run(main())

python-asyncio/rand.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import asyncio
2+
import random
3+
4+
COLORS = (
5+
"\033[0m", # End of color
6+
"\033[36m", # Cyan
7+
"\033[91m", # Red
8+
"\033[35m", # Magenta
9+
)
10+
11+
12+
async def main():
13+
return await asyncio.gather(
14+
makerandom(1, 9),
15+
makerandom(2, 8),
16+
makerandom(3, 8),
17+
)
18+
19+
20+
async def makerandom(delay, threshold=6):
21+
color = COLORS[delay]
22+
print(f"{color}Initiated makerandom({delay}).")
23+
while (number := random.randint(0, 10)) <= threshold:
24+
print(f"{color}makerandom({delay}) == {number} too low; retrying.")
25+
await asyncio.sleep(delay)
26+
print(f"{color}---> Finished: makerandom({delay}) == {number}" + COLORS[0])
27+
return number
28+
29+
30+
if __name__ == "__main__":
31+
random.seed(444)
32+
r1, r2, r3 = asyncio.run(main())
33+
print()
34+
print(f"r1: {r1}, r2: {r2}, r3: {r3}")

0 commit comments

Comments
 (0)