Skip to content

Commit 5da6c87

Browse files
committed
Updated code based on Geir's PR review. Implemented "most" of his suggestions.
1 parent 26cb0c1 commit 5da6c87

File tree

4 files changed

+14
-18
lines changed

4 files changed

+14
-18
lines changed

understanding-asynchronous-programming/example_3.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44

55

66
def task(name, queue):
7-
text = ''.join([f'Task {name} elapsed time: ', '{:.2f}'])
8-
timer = Timer(text=text)
7+
timer = Timer(text=f"Task {name} elapsed time: {{:.1f}}")
98
while not queue.empty():
109
delay = queue.get()
1110
print(f"Task {name} running")
@@ -19,18 +18,18 @@ def main():
1918
"""
2019
This is the main entry point for the program
2120
"""
22-
# Create the queue of 'work'
21+
# Create the queue of "work"
2322
work_queue = queue.Queue()
2423

25-
# Put some 'work' in the queue
24+
# Put some work in the queue
2625
for work in [15, 10, 5, 2]:
2726
work_queue.put(work)
2827

2928
tasks = [task("One", work_queue), task("Two", work_queue)]
3029

3130
# Run the tasks
3231
done = False
33-
with Timer(text='\nTotal elapsed time: {:.2f}'):
32+
with Timer(text="\nTotal elapsed time: {:.1f}"):
3433
while not done:
3534
for t in tasks:
3635
try:

understanding-asynchronous-programming/example_4.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33

44

55
async def task(name, work_queue):
6-
text = ''.join([f'Task {name} elapsed time: ', '{:.2f}'])
7-
timer = Timer(text=text)
6+
timer = Timer(text=f"Task {name} elapsed time: {{:.1f}}")
87
while not work_queue.empty():
98
delay = await work_queue.get()
109
print(f"Task {name} running")
@@ -17,15 +16,15 @@ async def main():
1716
"""
1817
This is the main entry point for the program
1918
"""
20-
# Create the queue of 'work'
19+
# Create the queue of "work"
2120
work_queue = asyncio.Queue()
2221

23-
# Put some 'work' in the queue
22+
# Put some "work" in the queue
2423
for work in [15, 10, 5, 2]:
2524
await work_queue.put(work)
2625

2726
# Run the tasks
28-
with Timer(text='\nTotal elapsed time: {:.2f}'):
27+
with Timer(text="\nTotal elapsed time: {:.1f}"):
2928
await asyncio.gather(
3029
asyncio.create_task(task("One", work_queue)),
3130
asyncio.create_task(task("Two", work_queue)),

understanding-asynchronous-programming/example_5.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44

55

66
def task(name, work_queue):
7-
text = ''.join([f'Task {name} elapsed time: ', '{:.2f}'])
8-
timer = Timer(text=text)
7+
timer = Timer(text=f"Task {name} elapsed time: {{:.1f}}")
98
with requests.Session() as session:
109
while not work_queue.empty():
1110
url = work_queue.get()
@@ -20,10 +19,10 @@ def main():
2019
"""
2120
This is the main entry point for the program
2221
"""
23-
# Create the queue of 'work'
22+
# Create the queue of "work"
2423
work_queue = queue.Queue()
2524

26-
# Put some 'work' in the queue
25+
# Put some "work" in the queue
2726
for url in [
2827
"http://google.com",
2928
"http://yahoo.com",
@@ -39,7 +38,7 @@ def main():
3938

4039
# Run the tasks
4140
done = False
42-
with Timer(text='\nTotal elapsed time: {:.2f}'):
41+
with Timer(text="\nTotal elapsed time: {:.1f}"):
4342
while not done:
4443
for t in tasks:
4544
try:

understanding-asynchronous-programming/example_6.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44

55

66
async def task(name, work_queue):
7-
text = ''.join([f'Task {name} elapsed time: ', '{:.2f}'])
8-
timer = Timer(text=text)
7+
timer = Timer(text=f"Task {name} elapsed time: {{:.1f}}")
98
async with aiohttp.ClientSession() as session:
109
while not work_queue.empty():
1110
url = await work_queue.get()
@@ -36,7 +35,7 @@ async def main():
3635
await work_queue.put(url)
3736

3837
# Run the tasks
39-
with Timer(text='\nTotal elapsed time: {:.2f}'):
38+
with Timer(text='\nTotal elapsed time: {:.1f}'):
4039
await asyncio.gather(
4140
asyncio.create_task(task("One", work_queue)),
4241
asyncio.create_task(task("Two", work_queue))

0 commit comments

Comments
 (0)