Skip to content

Commit 059eba2

Browse files
committed
asyncio native version (pre unsync)
1 parent 307ad77 commit 059eba2

File tree

1 file changed

+28
-7
lines changed

1 file changed

+28
-7
lines changed

apps/py/ch10_async/ascrape.py

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,30 @@
22
import httpx
33
import bs4
44
import datetime
5+
import asyncio
6+
7+
loop = asyncio.get_event_loop()
58

69

710
def main():
811
print("Python async web scraper")
912

1013
t0 = datetime.datetime.now()
11-
get_titles()
14+
loop.run_until_complete(get_titles())
1215
dt = datetime.datetime.now() - t0
1316
print(f"Finished in {dt.total_seconds():,.2f} seconds.")
1417

18+
loop.close()
19+
1520

16-
def get_html(n: int) -> str:
21+
async def get_html(n: int) -> str:
1722
print(Fore.YELLOW + f"Getting HTML for episode {n}...", flush=True)
1823
url = f'https://talkpython.fm/{n}'
1924

20-
resp = httpx.get(url)
21-
resp.raise_for_status()
25+
# The "async with" syntax ensures that all active connections are closed on exit.
26+
async with httpx.AsyncClient() as client:
27+
resp = await client.get(url)
28+
resp.raise_for_status()
2229

2330
return resp.text
2431

@@ -34,10 +41,24 @@ def get_title_from_html(n: int, html: str) -> str:
3441
return header.text.strip()
3542

3643

37-
def get_titles():
44+
# async def get_titles():
45+
# for n in range(220, 231):
46+
# html = await get_html(n)
47+
# title = get_title_from_html(n, html)
48+
# print(Fore.GREEN + title)
49+
50+
51+
async def get_titles():
52+
tasks = []
3853
for n in range(220, 231):
39-
html = get_html(n)
40-
title = get_title_from_html(n, html)
54+
task = loop.create_task(get_html(n))
55+
episode = n
56+
57+
tasks.append((episode, task))
58+
59+
for episode, task in tasks:
60+
html = await task
61+
title = get_title_from_html(episode, html)
4162
print(Fore.GREEN + title)
4263

4364

0 commit comments

Comments
 (0)