AWS Lambda loading libraries each time?

给你一囗甜甜゛ 提交于 2021-01-19 06:08:20

问题


I am looking at AWS Lambda to create a Python function that would process data. I need to load a heavy model to run my script (trained word2vec model), it takes about 5 min to do it on my computer for example. But once it's loaded, the execution of the function is very fast. If I use AWS Lambda, will this model load only once or will it load each time I call my function ?

Thanks,


回答1:


Maybe.

AWS Lambda uses reusable containers. So, for your use case, the Lambda function will execute quickly if it happened in an already initialized container. It'll be slow otherwise. However, there is no way you can predict the behavior.

Relevant documentation:

  1. From here:

The first time a function executes after being created or having its code or resource configuration updated, a new container with the appropriate resources will be created to execute it, and the code for the function will be loaded into the container.

Let’s say your function finishes, and some time passes, then you call it again. Lambda may create a new container all over again, in which case the experience is just as described above. This will be the case for certain if you change your code. However, if you haven’t changed the code and not too much time has gone by, Lambda may reuse the previous container.

Remember, you can’t depend on a container being reused, since it’s Lambda’s prerogative to create a new one instead.

  1. More official documentation here.



回答2:


It will MAYBE (thanks Michael-sqlbot for the correction) load each time you invoke Lambda.

We can infer that the AWS Lambdas are stateless based on the following

  1. Lambda is stateless

    "Lambda functions are 'stateless' with no affinity to the underlying infrastructure, so that Lambda can rapidly launch as many copies of the function as needed to scale to the rate of incoming events

  2. Lambda must be coded in stateless style

    Your Lambda function code must be written in a stateless style, and have no affinity with the underlying compute infrastructure. Your code should expect local file system access, child processes, and similar artifacts to be limited to the lifetime of the request

However Container reuse is possible in Lambda

If you haven’t changed the code and not too much time has gone by, Lambda may reuse the previous container

So basically to answer your question, it is possible that you get back the model, and the probability of that is inversely proportional to the time span between 2 Lambda invocations. But you simply cannot rely on that



来源:https://stackoverflow.com/questions/43065854/aws-lambda-loading-libraries-each-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!