[英]Why does my Python app always cold start twice on AWS lambda?
I have a lambda, in Python where I am loading a large machine learning model during the cold start.我有一个 lambda,在 Python 中,我正在冷启动期间加载大型机器学习 model。 The code is something like this:
代码是这样的:
uuid = uuid4()
app_logger.info("Loading model... %s" % uuid)
endpoints.embedder.load()
def create_app() -> FastAPI:
app = FastAPI()
app.include_router(endpoints.router)
return app
app_logger.info("Creating app... %s" % uuid)
app = create_app()
app_logger.info("Loaded app. %s" % uuid)
handler = Mangum(app)
The first time after deployment, AWS Lambda seems to start the Lambda twice as seen by the two different UUIDs.部署后的第一次,AWS Lambda 似乎启动了两次不同的 UUID 看到的 Lambda。 Here are the logs:
这是日志:
2023-01-05 21:44:40.083 | INFO | myapp.app:<module>:47 - Loading model... 76a5ac6f-a4fc-490e-b21c-83bb5ef458eb
2023-01-05 21:44:42.406 | INFO | myapp.embedder:load:31 - Loading embedding model
2023-01-05 21:44:50.626 | INFO | myapp.app:<module>:47 - Loading model... c633a9c6-bcfc-44d5-bacf-9834b39ee300
2023-01-05 21:44:51.878 | INFO | myapp.embedder:load:31 - Loading embedding model
2023-01-05 21:45:00.418 | INFO | myapp.app:<module>:59 - Creating app... c633a9c6-bcfc-44d5-bacf-9834b39ee300
2023-01-05 21:45:00.420 | INFO | myapp.app:<module>:61 - Loaded app. c633a9c6-bcfc-44d5-bacf-9834b39ee300
This happens consistently.这种情况一直发生。 It executes it for 10 seconds the first time, then seems to restart and do it again.
它第一次执行 10 秒,然后似乎重新启动并再次执行。 There are no errors in the logs that indicate why this would be.
日志中没有错误表明为什么会这样。 I have my Lambda configured to run with 4G of memory and it always loads with < 3GB used.
我将我的 Lambda 配置为使用 memory 的 4G 运行,并且它总是加载 < 3GB 使用。
Any ideas why this happens and how to avoid it?任何想法为什么会发生这种情况以及如何避免它?
To summarize all the learnings in the comments so far:总结到目前为止评论中的所有知识:
Perhaps someday SnapStart will address this problem for Python. Until then, I am going back to EC2.也许有一天 SnapStart 会为 Python 解决这个问题。在那之前,我将回到 EC2。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.