Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MultiThreading in AWS lambda using Python3

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.

like image 355
Sharvin26 Avatar asked Nov 20 '18 05:11

Sharvin26


People also ask

Can I use multithreading in AWS Lambda?

Using multithreading in AWS Lambda can speed up your Lambda execution and reduce cost as Lambda charges in 100 ms unit.

Does Python 3 support multithreading?

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.

Does AWS Lambda support parallel processing?

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).

Does AWS support Python 3?

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.


1 Answers

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
like image 146
Gabe Hollombe Avatar answered Oct 04 '22 08:10

Gabe Hollombe