Skip to content

Commit 2c70a60

Browse files
authored
Merge pull request #82 from realpython/understanding-async-python
Updating code to use Geir's codetiming module rather than my homegrow…
2 parents 8aa902c + 9679a38 commit 2c70a60

File tree

9 files changed

+57
-71
lines changed

9 files changed

+57
-71
lines changed

understanding-asynchronous-programming/example_1.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ def main():
1818
"""
1919
This is the main entry point for the program
2020
"""
21-
# Create the queue of 'work'
21+
# Create the queue of work
2222
work_queue = queue.Queue()
2323

24-
# Put some 'work' in the queue
24+
# Put some work in the queue
2525
for work in [15, 10, 5, 2]:
2626
work_queue.put(work)
2727

understanding-asynchronous-programming/example_2.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ def main():
1616
"""
1717
This is the main entry point for the program
1818
"""
19-
# Create the queue of 'work'
19+
# Create the queue of work
2020
work_queue = queue.Queue()
2121

22-
# Put some 'work' in the queue
22+
# Put some work in the queue
2323
for work in [15, 10, 5, 2]:
2424
work_queue.put(work)
2525

understanding-asynchronous-programming/example_3.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,43 @@
11
import time
22
import queue
3-
from lib.elapsed_time import ET
3+
from codetiming import Timer
44

55

66
def task(name, queue):
7+
timer = Timer(text=f"Task {name} elapsed time: {{:.1f}}")
78
while not queue.empty():
89
delay = queue.get()
9-
et = ET()
1010
print(f"Task {name} running")
11+
timer.start()
1112
time.sleep(delay)
12-
print(f"Task {name} total elapsed time: {et():.1f}")
13+
timer.stop()
1314
yield
1415

1516

1617
def main():
1718
"""
1819
This is the main entry point for the program
1920
"""
20-
# Create the queue of 'work'
21+
# Create the queue of work
2122
work_queue = queue.Queue()
2223

23-
# Put some 'work' in the queue
24+
# Put some work in the queue
2425
for work in [15, 10, 5, 2]:
2526
work_queue.put(work)
2627

2728
tasks = [task("One", work_queue), task("Two", work_queue)]
2829

2930
# Run the tasks
30-
et = ET()
3131
done = False
32-
while not done:
33-
for t in tasks:
34-
try:
35-
next(t)
36-
except StopIteration:
37-
tasks.remove(t)
38-
if len(tasks) == 0:
39-
done = True
40-
41-
print(f"\nTotal elapsed time: {et():.1f}")
32+
with Timer(text="\nTotal elapsed time: {:.1f}"):
33+
while not done:
34+
for t in tasks:
35+
try:
36+
next(t)
37+
except StopIteration:
38+
tasks.remove(t)
39+
if len(tasks) == 0:
40+
done = True
4241

4342

4443
if __name__ == "__main__":

understanding-asynchronous-programming/example_4.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,34 @@
11
import asyncio
2-
from lib.elapsed_time import ET
2+
from codetiming import Timer
33

44

55
async def task(name, work_queue):
6+
timer = Timer(text=f"Task {name} elapsed time: {{:.1f}}")
67
while not work_queue.empty():
78
delay = await work_queue.get()
8-
et = ET()
99
print(f"Task {name} running")
10+
timer.start()
1011
await asyncio.sleep(delay)
11-
print(f"Task {name} total elapsed time: {et():.1f}")
12+
timer.stop()
1213

1314

1415
async def main():
1516
"""
1617
This is the main entry point for the program
1718
"""
18-
# Create the queue of 'work'
19+
# Create the queue of work
1920
work_queue = asyncio.Queue()
2021

21-
# Put some 'work' in the queue
22+
# Put some work in the queue
2223
for work in [15, 10, 5, 2]:
2324
await work_queue.put(work)
2425

2526
# Run the tasks
26-
et = ET()
27-
await asyncio.gather(
28-
asyncio.create_task(task("One", work_queue)),
29-
asyncio.create_task(task("Two", work_queue)),
30-
)
31-
print(f"\nTotal elapsed time: {et():.1f}")
27+
with Timer(text="\nTotal elapsed time: {:.1f}"):
28+
await asyncio.gather(
29+
asyncio.create_task(task("One", work_queue)),
30+
asyncio.create_task(task("Two", work_queue)),
31+
)
3232

3333

3434
if __name__ == "__main__":

understanding-asynchronous-programming/example_5.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
11
import queue
22
import requests
3-
from lib.elapsed_time import ET
3+
from codetiming import Timer
44

55

66
def task(name, work_queue):
7+
timer = Timer(text=f"Task {name} elapsed time: {{:.1f}}")
78
with requests.Session() as session:
89
while not work_queue.empty():
910
url = work_queue.get()
1011
print(f"Task {name} getting URL: {url}")
11-
et = ET()
12+
timer.start()
1213
session.get(url)
13-
print(f"Task {name} total elapsed time: {et():.1f}")
14+
timer.stop()
1415
yield
1516

1617

1718
def main():
1819
"""
1920
This is the main entry point for the program
2021
"""
21-
# Create the queue of 'work'
22+
# Create the queue of work
2223
work_queue = queue.Queue()
2324

24-
# Put some 'work' in the queue
25+
# Put some work in the queue
2526
for url in [
2627
"http://google.com",
2728
"http://yahoo.com",
@@ -36,18 +37,16 @@ def main():
3637
tasks = [task("One", work_queue), task("Two", work_queue)]
3738

3839
# Run the tasks
39-
et = ET()
4040
done = False
41-
while not done:
42-
for t in tasks:
43-
try:
44-
next(t)
45-
except StopIteration:
46-
tasks.remove(t)
47-
if len(tasks) == 0:
48-
done = True
49-
50-
print(f"\nTotal elapsed time: {et():.1f}")
41+
with Timer(text="\nTotal elapsed time: {:.1f}"):
42+
while not done:
43+
for t in tasks:
44+
try:
45+
next(t)
46+
except StopIteration:
47+
tasks.remove(t)
48+
if len(tasks) == 0:
49+
done = True
5150

5251

5352
if __name__ == "__main__":

understanding-asynchronous-programming/example_6.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
11
import asyncio
22
import aiohttp
3-
from lib.elapsed_time import ET
3+
from codetiming import Timer
44

55

66
async def task(name, work_queue):
7+
timer = Timer(text=f"Task {name} elapsed time: {{:.1f}}")
78
async with aiohttp.ClientSession() as session:
89
while not work_queue.empty():
910
url = await work_queue.get()
1011
print(f"Task {name} getting URL: {url}")
11-
et = ET()
12+
timer.start()
1213
async with session.get(url) as response:
1314
await response.text()
14-
print(f"Task {name} total elapsed time: {et():.1f}")
15+
timer.stop()
1516

1617

1718
async def main():
1819
"""
1920
This is the main entry point for the program
2021
"""
21-
# Create the queue of 'work'
22+
# Create the queue of work
2223
work_queue = asyncio.Queue()
2324

24-
# Put some 'work' in the queue
25+
# Put some work in the queue
2526
for url in [
2627
"http://google.com",
2728
"http://yahoo.com",
@@ -34,12 +35,11 @@ async def main():
3435
await work_queue.put(url)
3536

3637
# Run the tasks
37-
et = ET()
38-
await asyncio.gather(
39-
asyncio.create_task(task("One", work_queue)),
40-
asyncio.create_task(task("Two", work_queue)),
41-
)
42-
print(f"\nTotal elapsed time: {et():.1f}")
38+
with Timer(text="\nTotal elapsed time: {:.1f}"):
39+
await asyncio.gather(
40+
asyncio.create_task(task("One", work_queue)),
41+
asyncio.create_task(task("Two", work_queue)),
42+
)
4343

4444

4545
if __name__ == "__main__":

understanding-asynchronous-programming/lib/__init__.py

Whitespace-only changes.

understanding-asynchronous-programming/lib/elapsed_time.py

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

understanding-asynchronous-programming/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ async-timeout==3.0.1
33
attrs==19.1.0
44
certifi==2019.3.9
55
chardet==3.0.4
6+
codetiming==0.1.2
67
idna==2.8
78
multidict==4.5.2
89
requests==2.21.0

0 commit comments

Comments
 (0)