Skip to content

The variable outside lambda handler method is not preserved in the subsequences requests #79

@phuclh

Description

@phuclh

I have a python script like this:

model = None

def get_model():
    global model

    if model is not None:
        return model

    model = SentenceTransformer('all-MiniLM-L6-v2')

    return model

def lambda_handler(event, context):
    model = get_model()

When the model = SentenceTransformer('all-MiniLM-L6-v2') is initialized it will download the library and this process takes a while. So I put it in the get_model method and return the model from the previous request for subsequence requests to save time.

However, when I execute the lambda function via sidecar within a route.

\App\Sidecar\Functions\Test::execute()

I got timeout for that request, so I put that code into a job and run it in a queue. This time, it ran successfully and I expected the get_model method would return the model from the 1st run in the queue when I re-visit the above route, but it didn't. However, it did return when I dispatched another job to queue (I knew that because this time the job ran much faster).

So I wonder if AWS Lambda creates a different instance for QUEUE and HTTP Request?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions