我的项目包括使用构建在 aws lambda 服务之上的 api。从技术上讲,构建 api 的负责人告诉我,由于服务是弹性的,因此没有固定的请求限制,但重要的是要考虑 api 可以支持的每秒请求数。
为了控制每秒请求的限制(同时),我正在开发的 python 脚本使用 asyncio 和 httpx 来同时使用 api,并利用httpx.Limits的 max_connections 参数,我试图找到最佳值,以便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 = …Run Code Online (Sandbox Code Playgroud)