Skip to content

Commit 3ce7e86

Browse files
authored
Fixes talkpython#5
1 parent 5114a7b commit 3ce7e86

File tree

1 file changed

+6
-1
lines changed
  • src/04-asyncio/web_scraping/async_scrape

1 file changed

+6
-1
lines changed

src/04-asyncio/web_scraping/async_scrape/program.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
import bs4
55
from colorama import Fore
66

7+
# Older versions of python require calling loop.create_task() rather than on asyncio.
8+
# Make this available more easily.
9+
global loop
10+
711

812
async def get_html(episode_number: int) -> str:
913
print(Fore.YELLOW + f"Getting HTML for episode {episode_number}", flush=True)
@@ -28,6 +32,7 @@ def get_title(html: str, episode_number: int) -> str:
2832

2933

3034
def main():
35+
global loop
3136
loop = asyncio.get_event_loop()
3237
loop.run_until_complete(get_title_range())
3338
print("Done.")
@@ -46,7 +51,7 @@ async def get_title_range():
4651

4752
tasks = []
4853
for n in range(150, 160):
49-
tasks.append((n, asyncio.create_task(get_html(n))))
54+
tasks.append((n, loop.create_task(get_html(n))))
5055

5156
for n, t in tasks:
5257
html = await t

0 commit comments

Comments
 (0)