Parvathy
Parvathy

Reputation: 191

How I can invoke lambda with a delay?

How I can invoke lambda with a delay in python? After execute some line of code in lambda function, I need to sleep the function for 5 sec and then continue.

How can i do this in python. I tried the simple way sleep(5), but it doesn't work.

Upvotes: 1

Views: 25468

Answers (4)

vijayraj34
vijayraj34

Reputation: 2415

In order to make use of sleep(10) timeout in lambda, make sure you increase the timeout configuration from 3sec (default).

How to change?

Configuration Tab -> General Configuration -> Edit -> Timeout (N-min, N-sec). e.g. 5min 0sec.

In my case, I wanted a maximum wait time of 1-3minutes. For which i kept the timeout to 5 min. Which is in range.

Upvotes: 0

mariux
mariux

Reputation: 3117

Using sleep() in a lambda function will make you pay for nothing. Think about refactoring your code to execute as fast as possible and not adding additional sleeps in lambda functions (or in code at all ;)).

That said, a simple

import time
time.sleep(5)

should actually make your code sleep for 5 seconds in python - no matter if executed in a lambda or not. Ensure the timeout of your lambda matches the sum of your execution time and sleep time to not get unexpected timeouts.

Upvotes: 5

Nikhil K Murali
Nikhil K Murali

Reputation: 151

You can use the sleep() method to create delay in the code execution. First you need to import import time to your function and add the time method in your code.

import time

time.sleep(5) # this will make the execution sleep for 5 seconds

Please note that the Default time out of AWS Lambda function is 3 seconds so, increase the timeout time which is more than 5 seconds say 8 seconds else your code execution will timeout.

Upvotes: 1

Bob Confrancisco
Bob Confrancisco

Reputation: 21

Double check that your lambda function basic settings does not have a timeout less than 5 seconds. I think it defaults to 3.

Upvotes: 2

Related Questions