我的项目包括使用构建在aws服务之上的api。从技术上讲,构建api的领导者告诉我,由于服务是有弹性的,所以没有固定的请求限制,但是重要的是要考虑api能够支持的每秒请求数量。
为了控制每秒请求的限制(并发),我正在开发的python脚本使用异步和httpx并发地使用httpx.Limits,并利用httpx.Limits的httpx.Limits参数,试图找到最优的值,这样api就不会冻结。
我的问题是,我不知道我是否误解了max_connections参数的使用,因为当测试值为1000时,我的理解告诉我,每秒我要向api并发发出1000个请求,但即便如此,经过一段时间之后,api仍然会冻结。
我希望能够控制每秒请求的限制,而不需要使用第三方库。
我怎么能做到呢?
这是我的MWE
async def consume(client, endpoint: str = '/create', reg):
data = {"param1": reg[1]}
response = await client.post(url=endpoint, data=json.dumps(data))
return response.json()
async def run(self, regs):
# Empty list to consolidate all responses
results = []
# httpx limits configuration
limits = httpx.Limits(max_keepalive_connections=None, max_connections=1000)
timeout = httpx.Timeout(connect=60.0, read=30.0, write=30.0, pool=60.0)
# httpx client context
async with httpx.AsyncClient(base_url='https://apiexample', headers={'Content-Type': 'application/json'},
limits=limits, timeout=timeout) as client:
# regs is a list of more than 1000000 tuples
tasks = [asyncio.ensure_future(consume(client=client, reg=reg))
for reg in regs]
result = await asyncio.gather(*tasks)
results += result
return results提前谢谢。
发布于 2022-10-10 16:16:13
你的领导错了-- 对于AWS lambda有一个请求限制。 (默认情况下是1000次并行执行)。
AWS不太可能“冻结”(有很多层的保护),所以我会在您的端查找一个问题。通过降低“一致连接”设置(例如100)开始调试,如果没有解决问题,则探索其他设置。
更多信息:https://www.bluematador.com/blog/why-aws-lambda-throttles-functions
https://stackoverflow.com/questions/71517645
复制相似问题