如何对所有 aws lambda 请求使用单个 pymongo 连接池
How to use single pymongo connection pool for all aws lambda requests
我正在使用 aws lambda 函数和 API 网关以及 python 来处理 HTTP 请求。
我应该在每次请求后关闭 pymongo 连接还是有办法使用公共连接池?
如果我们为每个具有 5 个连接的 http 请求使用连接池,lambda 上的数千个请求将打开 5*1000=5000 mongodb 个连接。
据我所知,不可能在函数调用之间传输状态,但您可以在 event_handler
函数之前初始化您的数据库连接,它将被重新用于此特定函数实例的下一次热启动。如果所有请求同时启动,可以限制同时启动的最大实例数,强制重用已经启动的功能。
这里是 link,描述了一些最佳实践:
Take advantage of execution environment reuse to improve the performance of
your function. Initialize SDK clients and database connections outside
of the function handler, and cache static assets locally in the /tmp
directory. Subsequent invocations processed by the same instance of
your function can reuse these resources. This saves cost by reducing
function run time.
我正在使用 aws lambda 函数和 API 网关以及 python 来处理 HTTP 请求。
我应该在每次请求后关闭 pymongo 连接还是有办法使用公共连接池?
如果我们为每个具有 5 个连接的 http 请求使用连接池,lambda 上的数千个请求将打开 5*1000=5000 mongodb 个连接。
据我所知,不可能在函数调用之间传输状态,但您可以在 event_handler
函数之前初始化您的数据库连接,它将被重新用于此特定函数实例的下一次热启动。如果所有请求同时启动,可以限制同时启动的最大实例数,强制重用已经启动的功能。
这里是 link,描述了一些最佳实践:
Take advantage of execution environment reuse to improve the performance of your function. Initialize SDK clients and database connections outside of the function handler, and cache static assets locally in the /tmp directory. Subsequent invocations processed by the same instance of your function can reuse these resources. This saves cost by reducing function run time.