我正在尝试同时提出多个请求。
我对 Python 中的
async
和 await
很陌生(我在 Js 中使用过它)。
我找到了一个例子并使用了这个:
import asyncio
import aiohttp
async def get_item( session: aiohttp.ClientSession, path: str ):
# fetch data
url = f'my/path/here{path}'
resp = await session.request('GET', url=url)
data = await resp.json()
# at this point I do stuff to the data and save as csv
return await data
async def get_all():
tasks = []
async with aiohttp.ClientSession() as session:
tasks.append(get_item(session=session, path='1'))
tasks.append(get_item(session=session, path='2'))
return await asyncio.gather(*tasks, return_exceptions=True)
loop = asyncio.get_event_loop()
loop.run_until_complete(get_all())
但我收到以下错误:
RuntimeError: This event loop is already running
显然有一个问题,请有人解释一下问题是什么以及如何解决这个问题?
这是在我的计算机上运行的重组版本(主要是将
asyncio.gather
放在with
下并从await
中删除return data
:
import asyncio
import aiohttp
async def get_item(session: aiohttp.ClientSession, path: str):
# fetch data
url = "https://www.google.com"
resp = await session.request("GET", url=url)
data = (await resp.text())[:15] # <-- get first 15 chars just for example
return data # <-- remove await here
async def get_all():
tasks = []
async with aiohttp.ClientSession() as session:
tasks.append(get_item(session=session, path="1"))
tasks.append(get_item(session=session, path="2"))
return await asyncio.gather(
*tasks, return_exceptions=True
) # <-- move gather under `with` (to not close session prematurely)
loop = asyncio.get_event_loop()
print(loop.run_until_complete(get_all()))
打印:
['<!doctype html>', '<!doctype html>']