Skip to content

Commit 03ab1e7

Browse files
committed
basic unsync
1 parent 059eba2 commit 03ab1e7

File tree

3 files changed

+7
-8
lines changed

3 files changed

+7
-8
lines changed

apps/py/.idea/dictionaries/screencaster.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

apps/py/ch10_async/ascrape.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,19 @@
22
import httpx
33
import bs4
44
import datetime
5-
import asyncio
6-
7-
loop = asyncio.get_event_loop()
5+
from unsync import unsync
86

97

108
def main():
119
print("Python async web scraper")
1210

1311
t0 = datetime.datetime.now()
14-
loop.run_until_complete(get_titles())
12+
get_titles().result()
1513
dt = datetime.datetime.now() - t0
1614
print(f"Finished in {dt.total_seconds():,.2f} seconds.")
1715

18-
loop.close()
19-
2016

17+
@unsync
2118
async def get_html(n: int) -> str:
2219
print(Fore.YELLOW + f"Getting HTML for episode {n}...", flush=True)
2320
url = f'https://talkpython.fm/{n}'
@@ -47,11 +44,11 @@ def get_title_from_html(n: int, html: str) -> str:
4744
# title = get_title_from_html(n, html)
4845
# print(Fore.GREEN + title)
4946

50-
47+
@unsync
5148
async def get_titles():
5249
tasks = []
5350
for n in range(220, 231):
54-
task = loop.create_task(get_html(n))
51+
task = get_html(n)
5552
episode = n
5653

5754
tasks.append((episode, task))

apps/py/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ requests
88
pytest
99
pytest-mock
1010
pytest-clarity
11+
unsync

0 commit comments

Comments
 (0)