Skip to content

Commit 1666e7b

Browse files
committed
Post final QA fixes
1 parent 41a1a7e commit 1666e7b

File tree

10 files changed

+77
-64
lines changed

10 files changed

+77
-64
lines changed

python-asyncio/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
# Async I/O in Python: A Complete Walkthrough
1+
# Python's `asyncio`: A Hands-On Walkthrough
22

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/).
3+
This folder provides the code examples for the Real Python tutorial [Python's `asyncio`: A Hands-On Walkthrough](https://realpython.com/async-io-python/).

python-asyncio/as_completed.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@
22
import time
33

44

5-
async def coro(numbers):
6-
await asyncio.sleep(min(numbers))
7-
return list(reversed(numbers))
8-
9-
105
async def main():
116
task1 = asyncio.create_task(coro([10, 5, 2]))
127
task2 = asyncio.create_task(coro([3, 2, 1]))
@@ -18,4 +13,10 @@ async def main():
1813
print(f"Both tasks done: {all((task1.done(), task2.done()))}")
1914

2015

21-
asyncio.run(main())
16+
async def coro(numbers):
17+
await asyncio.sleep(min(numbers))
18+
return list(reversed(numbers))
19+
20+
21+
if __name__ == "__main__":
22+
asyncio.run(main())

python-asyncio/countasync.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import asyncio
22

33

4+
async def main():
5+
await asyncio.gather(count(), count(), count())
6+
7+
48
async def count():
59
print("One")
610
await asyncio.sleep(1)
711
print("Two")
812
await asyncio.sleep(1)
913

1014

11-
async def main():
12-
await asyncio.gather(count(), count(), count())
13-
14-
1515
if __name__ == "__main__":
1616
import time
1717

python-asyncio/countsync.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
import time
22

33

4+
def main():
5+
for _ in range(3):
6+
count()
7+
8+
49
def count():
510
print("One")
611
time.sleep(1)
712
print("Two")
813
time.sleep(1)
914

1015

11-
def main():
12-
for _ in range(3):
13-
count()
14-
15-
1616
if __name__ == "__main__":
1717
start = time.perf_counter()
1818
main()

python-asyncio/except_group.py

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
import asyncio
22

33

4+
async def main():
5+
results = await asyncio.gather(
6+
coro_a(), coro_b(), coro_c(), return_exceptions=True
7+
)
8+
exceptions = [e for e in results if isinstance(e, Exception)]
9+
if exceptions:
10+
raise ExceptionGroup("Errors", exceptions)
11+
12+
413
async def coro_a():
514
await asyncio.sleep(1)
615
raise ValueError("Error in coro A")
@@ -16,20 +25,12 @@ async def coro_c():
1625
raise IndexError("Error in coro C")
1726

1827

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}")
28+
if __name__ == "__main__":
29+
try:
30+
asyncio.run(main())
31+
except* ValueError as ve_group:
32+
print(f"[ValueError handled] {ve_group.exceptions}")
33+
except* TypeError as te_group:
34+
print(f"[TypeError handled] {te_group.exceptions}")
35+
except* IndexError as ie_group:
36+
print(f"[IndexError handled] {ie_group.exceptions}")

python-asyncio/gathers.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@
22
import time
33

44

5-
async def coro(numbers):
6-
await asyncio.sleep(min(numbers))
7-
return list(reversed(numbers))
8-
9-
105
async def main():
116
task1 = asyncio.create_task(coro([10, 5, 2]))
127
task2 = asyncio.create_task(coro([3, 2, 1]))
@@ -17,5 +12,11 @@ async def main():
1712
return result
1813

1914

20-
result = asyncio.run(main())
21-
print(f"result: {result}")
15+
async def coro(numbers):
16+
await asyncio.sleep(min(numbers))
17+
return list(reversed(numbers))
18+
19+
20+
if __name__ == "__main__":
21+
result = asyncio.run(main())
22+
print(f"result: {result}")

python-asyncio/powers.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
import asyncio
22

33

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-
124
async def main():
135
g = []
146
async for i in powers_of_two(5):
157
g.append(i)
168
print(g)
17-
189
f = [j async for j in powers_of_two(5) if not (j // 3 % 5)]
1910
print(f)
2011

2112

13+
async def powers_of_two(stop=10):
14+
exponent = 0
15+
while exponent < stop:
16+
yield 2**exponent
17+
exponent += 1
18+
await asyncio.sleep(0.2) # Simulate some asynchronous work
19+
20+
2221
if __name__ == "__main__":
2322
asyncio.run(main())

python-asyncio/requirements.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
aiohappyeyeballs==2.6.1
2+
aiohttp==3.12.14
3+
aiosignal==1.4.0
4+
attrs==25.3.0
5+
frozenlist==1.7.0
6+
idna==3.10
7+
multidict==6.6.3
8+
propcache==0.3.2
9+
yarl==1.20.1

python-asyncio/tasks.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
import asyncio
22

33

4-
async def coro(numbers):
5-
await asyncio.sleep(min(numbers))
6-
return list(reversed(numbers))
7-
8-
94
async def main():
105
task = asyncio.create_task(coro([3, 2, 1]))
116
print(f"{type(task) = }")
127
print(f"{task.done() = }")
138
return await task
149

1510

16-
result = asyncio.run(main())
17-
print(f"result: {result}")
11+
async def coro(numbers):
12+
await asyncio.sleep(min(numbers))
13+
return list(reversed(numbers))
14+
15+
16+
if __name__ == "__main__":
17+
result = asyncio.run(main())
18+
print(f"result: {result}")

python-asyncio/websites.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,6 @@
33
import aiohttp
44

55

6-
async def check(url):
7-
async with aiohttp.ClientSession() as session:
8-
async with session.get(url) as response:
9-
print(f"{url}: status -> {response.status}")
10-
11-
126
async def main():
137
websites = [
148
"https://realpython.com",
@@ -18,4 +12,11 @@ async def main():
1812
await asyncio.gather(*(check(url) for url in websites))
1913

2014

21-
asyncio.run(main())
15+
async def check(url):
16+
async with aiohttp.ClientSession() as session:
17+
async with session.get(url) as response:
18+
print(f"{url}: status -> {response.status}")
19+
20+
21+
if __name__ == "__main__":
22+
asyncio.run(main())

0 commit comments

Comments
 (0)