简体   繁体   English

为什么我的 Python 应用程序总是在 AWS lambda 上冷启动两次?

[英]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:总结到目前为止评论中的所有知识:

  • AWS limits the init phase to 10 seconds. AWS 将初始阶段限制为 10 秒。 This is explained here: https://docs.aws.amazon.com/lambda/latest/dg/lambda-runtime-environment.html这在这里解释: https://docs.aws.amazon.com/lambda/latest/dg/lambda-runtime-environment.html
  • If the app exceeds 10 seconds, it gets inited again without this limit如果应用程序超过 10 秒,它会在没有此限制的情况下再次启动
  • If you hit the 10 second limit, there are two ways to deal with this:如果您达到 10 秒的限制,有两种方法可以解决这个问题:
    • Init the model after the function is loaded during the invocation.在调用期间加载 function 后初始化 model。 The downsides being that you don't get the CPU boost and lower cost initialization.缺点是您无法获得 CPU 提升和较低的初始化成本。
    • Use provisioned concurrency.使用预配的并发。 Init is not limited to 10 seconds, but this is more expensive and can still run into the same problems as not using it, eg if you get a burst in usage. Init 不限于 10 秒,但这更昂贵并且仍然会遇到与不使用它相同的问题,例如,如果您的使用量激增。
  • Moving my model to EFS does improve startup time compared to S3 and Docker layer caching, but it is not sufficient to make it init in < 10 seconds.与 S3 和 Docker 层缓存相比,将我的 model 移至 EFS 确实缩短了启动时间,但不足以使其在 < 10 秒内初始化。 It might work for other use cases with slightly smaller models though.不过,它可能适用于模型稍小的其他用例。

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.

相关问题 AWS Lambda 层:它是否有冷启动与主要 lambda 分开? - AWS Lambda Layers: does it have a cold start separated from the main lambda? 如何使用 Zappa 解决 AWS Lambda 中的冷启动问题? - How to overcome cold start issue in AWS Lambda with Zappa? 为什么 settimeout 在我的 aws lambda 中导致 502? - Why does settimeout cause a 502 in my aws lambda? AWS Lambda 触发两次 - AWS Lambda Firing twice 如何在 Python 中模拟 AWS lambda start_execution? - How do I mock an AWS lambda start_execution in Python? 为什么我的 AWS lambda function 在使用私有 elasticache.network 调用以及外部 API 调用时随机失败? - Why Does my AWS lambda function randomly fail when using private elasticache network calls as well as external API calls? 为什么我的 AWS Lambda function 在没有超时消息的情况下结束? - Why is my AWS Lambda function ending before finishing with no timeout message? 为什么带有 Chrome 驱动程序的 Selenium 在本地工作但在 AWS Lambda 上崩溃? - Why does Selenium with Chrome driver work locally but crashes on AWS Lambda? AWS Lambda 为单个 SQS 消息触发两次 - AWS Lambda triggered twice for a sigle SQS Message Bazel 使用 python 生成 AWS lambda - Bazel generate AWS lambda with python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM