每个URL都有自己的会话吗?我阅读了在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)发布于 2018-12-08 23:07:55
每个URL都有自己的会话吗?
是的,只需将会话创建移动到fetch协同线:
async def fetch(site):
print('Fetching: ' + site)
async with aiohttp.ClientSession() as session, \
session.get(site) as response:
return await response.text()
async def main():
t = []
for site in sites:
task = asyncio.create_task(fetch(site))
t.append(task)
await asyncio.gather(*t)https://stackoverflow.com/questions/53686189
复制相似问题