【发布时间】:2021-10-01 09:51:57
【问题描述】:
我已查看此帖子(但无法回复):aiohttp: rate limiting parallel requests
首先,我要感谢@Sraw 在上述帖子中的cmets!
但是,我怀疑我目前的解决方案是否做得很好:
for convertedJson in allJsons:
.....
.....
tasks.append(asyncio.ensure_future(fetch(env,typeRequest,convertedJson,headers,retries,execution,session)))
await asyncio.sleep(1/800)
results = await asyncio.gather(*tasks,return_exceptions=True)
await session.close()
在我的 fetch 函数中,我基本上是这样做的:
async def fetch(env,typeRequest,request,headers,retries,execution,session):
.....
.....
async with session.post(url=VALIDATION_API_ENDPOINT,json=request["body_request"],headers=headers,timeout=None,allow_redirects=False) as response:
......
......
jsonF=await response.json()
return jsonF
问题是我想每秒发出 800 个请求。我做得对吗?我想很明显,我无法知道收到回复需要多长时间。我想知道我是否已经按照这种逻辑执行了这 800 个请求/秒,因为服务器人员告诉我,我实际上正在执行大约 400 个请求/秒(对我来说没有意义)。
谢谢大家! BR,
【问题讨论】:
标签: python api request python-asyncio aiohttp