I am trying to implement Multithreading in AWS lambda. This is a Sample code that defines the format of my original code which I am trying to execute in lambda.
import threading
import time
def this_will_await(arg,arg2):
print("Hello User")
print(arg,arg2)
def this_should_start_then_wait():
print("This starts")
timer = threading.Timer(3.0, this_will_await,["b","a"])
timer.start()
print("This should execute")
this_should_start_then_wait()
In my local Machine, this code is working fine. The output I am receiving is:
This starts
This should execute
.
.
.
Hello User
('b', 'a')
Those 3 . represents that it waited for 3 seconds to complete the execution.
Now when I execute the same thing in AWS lambda. I am only receiving
This starts
This should execute
I think it's not calling the this_will_await() function.
Using multithreading in AWS Lambda can speed up your Lambda execution and reduce cost as Lambda charges in 100 ms unit.
Yes. Python programming language supports multithreading. We can do multithreading in Python by importing the module called 'threading' and using the class 'Thread'. If you want to know how multithreading works in Python, just stay around.
No. AWS Lambda is designed to run many instances of your functions in parallel. However, AWS Lambda has a default safety throttle for number of concurrent executions per account per region (visit here for info on default safety throttle limits).
You can now use the Python 3.9 runtime to develop your AWS Lambda functions. To use this version, specify a runtime parameter value python3.
Have you tried adding timer.join()
? You'll need to join the Timer thread because otherwise the Lambda environment will kill off the thread when the parent thread finishes.
This code in a Lambda function:
import threading
import time
def this_will_await(arg,arg2):
print("Hello User")
print(arg,arg2)
def this_should_start_then_wait():
print("This starts")
timer = threading.Timer(3.0, this_will_await,["b","a"])
timer.start()
timer.join()
print("This should execute")
this_should_start_then_wait()
def lambda_handler(event, context):
return this_should_start_then_wait()
Produces this output:
This starts
Hello User
b a
This should execute
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With