Home > Enterprise >  AWS Lambda function not running concurrently
AWS Lambda function not running concurrently

Time:05-11

I have an AWS Lambda function that gets invoked from another function. The first function processes the data and invokes the other when it is finished. The second function will get n instances to run at the same time.

For example the second function takes about 5 seconds (for each invoke) to run; I want this function to run all at the time they are invoked for a total run time of about 5 seconds.

The function takes longer than that and runs each function one at a time until the one prior is finished; this process takes 5*n seconds.

I see that I can scale the function to run up to 1,000 in my region as stated by AWS. How can I make this run concurrently? Don't need a code example, just a general process I can look into to fix the problem.

The first function header looks like this: (I have other code that gets the json_file that I left out)

def lambda_handler(event=None, context=None):

for n in range(len(json_file)):

    response = client.invoke(
        FunctionName='docker-selenium-lambda-prod-demo',
        InvocationType='RequestResponse',
        Payload=json.dumps(json_file[n])
        )

    responseJson = json.load(response['Payload'])

where json_file[n] is being sent to the other function to run.

CodePudding user response:

As you can see in boto3 docs about invoke function:

Invokes a Lambda function. You can invoke a function synchronously (and wait for the response), or asynchronously. To invoke a function asynchronously, set InvocationType to Event .

If you are using RequestResponse, your code will wait until the lambda called is terminated.

You can either change InvocationType to Event or use something like ThreadPoolExecutor and wait until all executions are finished

  • Related