Joa*_*oan 5 python-3.x python-asyncio httpx
我的项目包括使用构建在 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 = 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
Run Code Online (Sandbox Code Playgroud)
提前致谢。
您的领导者错了 - AWS lambda 存在请求限制(默认情况下为 1000 个并发执行)。
AWS API 不太可能“冻结”(有很多层保护),因此我会寻找您这边的问题。通过降低并发连接设置(例如 100)开始调试,如果这不能解决问题,请探索其他设置。
更多信息:https ://www.bluematador.com/blog/why-aws-lambda-throttles-functions
| 归档时间: |
|
| 查看次数: |
2405 次 |
| 最近记录: |