Skip to content

Commit 360e8a0

Browse files
committed
Made the example use the requests.Session() context manager, as suggested by Brad Solomon. :)
1 parent c808972 commit 360e8a0

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

understanding-asynchronous-programming/example_5.py

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

55

66
def task(name, work_queue):
7-
while not work_queue.empty():
8-
url = work_queue.get()
9-
print(f"Task {name} getting URL: {url}")
10-
et = ET()
11-
requests.get(url)
12-
print(f"Task {name} total elapsed time: {et():.1f}")
13-
yield
7+
with requests.Session() as session:
8+
while not work_queue.empty():
9+
url = work_queue.get()
10+
print(f"Task {name} getting URL: {url}")
11+
et = ET()
12+
session.get(url)
13+
print(f"Task {name} total elapsed time: {et():.1f}")
14+
yield
1415

1516

1617
def main():

0 commit comments

Comments
 (0)