Issue
W.r.t. Łukasz' tutorial on Youtube for a simple web-crawler, the following code gives RuntimeError: Event loop is closed
. This happens after the code runs successfully and prints out the time taken to complete the program.
import asyncio
import time
from typing import Callable, Coroutine
import httpx
addr = 'https://langa.pl/crawl'
async def progress(
url: str,
algo: Callable[..., Coroutine],
) -> None:
# store create_task into a variable
task = asyncio.create_task(
algo(url),
name=url,
)
todo.add(task) # add task to todo
start = time.time()
while len(todo):
done, _pending = await asyncio.wait(todo, timeout=0.5)
# cleanup todo by removing the done task
todo.difference_update(done)
# report completed urls
urls = (t.get_name() for t in todo)
# prints the current status
print(f"{len(todo)}: " + " ".join(sorted(urls))[-75:])
end = time.time()
print(f"Took {int(end-start)} seconds")
async def crawl3(
prefix: str, url: str = "",
) -> None:
url = url or prefix
client = httpx.AsyncClient()
try:
res = await client.get(url)
finally:
await client.aclose()
for line in res.text.splitlines():
if line.startswith(prefix):
task = asyncio.create_task(
crawl3(prefix, line),
name=line,
)
todo.add(task)
todo = set()
asyncio.run(progress(addr, crawl3))
Why is this happening? What needs to be changed in the code?
Solution
Resolved from pointer given by @user4815162342 - being tracked in this issue
import asyncio
import sys ## added to prevent Event Loop exception
import time
from typing import Callable, Coroutine
import httpx
## added to prevent Event Loop exception...
if sys.version_info[0] == 3 and sys.version_info[1] >= 8 and sys.platform.startswith('win'):
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
addr = 'https://langa.pl/crawl'
...
Answered By - reservoirinvest
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.