我来自C#背景,Python的Asyncio库使我感到困惑。
我已经阅读了以下1 2,但是对我来说,使用asyncio仍然不清楚。
我正在尝试使用异步的python创建网站抓取工具。
async def requestPage(url):
request = requests.get(url, headers=headers)
soup = BeautifulSoup(request.content, 'html.parser')
return soup
async def main():
#****** How do I run an async task and store its result to use in another task?
index_soup = asyncio.ensure_future(requestPage(index_url))
res = asyncio.gather(index_soup)
currency_urls = res.select('a[href^="/currencies"]')
print(currency_urls)
loop = asyncio.get_event_loop()
try:
loop.run_until_complete(main())
finally:
loop.close()
答案 0 :(得分:0)
由于请求库不是异步的,因此可以使用run_in_executor方法,因此它不会阻塞正在运行的线程。结果,您可以将requestPage
定义为常规函数,并在main
函数中调用它,如下所示:
res = await asyncio.gather(loop.run_in_executor(None, requestPage, url)
阻止功能将在单独的执行器中运行,而控件将返回到事件循环。
或者您可以尝试使用异步HTTP客户端库,例如aiohttp。
答案 1 :(得分:-1)
好的,我想我找到了基本的解决方案。
await
我写了一个包装程序,使我可以异步调用该函数并存储结果。