有没有办法让每个网址都有自己的会话?我阅读了在Github上找到的aiohttp文档,但我无法找到这是否可行 . 我知道这可能是请求但不确定如何使用aiohttp这样做 . 任何帮助表示赞赏,因为我无法找到答案 .
sites = ['http://example.com/api/1', 'http://example.com/api/2']
async def fetch(session, site):
print('Fetching: ' + site)
async with session.get(site) as response:
return await response.text()
async def main():
t = []
async with aiohttp.ClientSession() as session:
for site in sites:
task = asyncio.create_task(fetch(session, site))
t.append(task)
await asyncio.gather(*t)
1 回答
是的,只需将会话创建移动到
fetch
协程: