https://medium.com/@jun711.g/aws-lambda-and-multi-threading-using-python-62352d632e
import json
from concurrent.futures import \
ThreadPoolExecutor, as_completed
# task that is performed in parallel
def task(index):
return ['processes', index], index
def lambda_handler(event, context):
# TODO implement
all_tasks = []
list_of_items = [i for i in range(20)]
with ThreadPoolExecutor(max_workers=4) as executor:
for item_index in range(len(list_of_items)):
all_tasks.append(
executor.submit(
task,
item_index)
)
temp_res = list(range(len(list_of_items)))
# process completed tasks
for future in as_completed(all_tasks):
rs, index = future.result()
temp_res[index] = rs
for t in temp_res:
print("t", t)
return {
'statusCode': 200,
'body': json.dumps('Hello from Lambda!')
}
Function Logs
START RequestId: a0fb21f3-e3f3-4524-8bb8-13eba8d2bb43 Version: $LATEST
t ['processes', 0]
t ['processes', 1]
t ['processes', 2]
t ['processes', 3]
t ['processes', 4]
t ['processes', 5]
t ['processes', 6]
t ['processes', 7]
t ['processes', 8]
t ['processes', 9]
t ['processes', 10]
t ['processes', 11]
t ['processes', 12]
t ['processes', 13]
t ['processes', 14]
t ['processes', 15]
t ['processes', 16]
t ['processes', 17]
t ['processes', 18]
t ['processes', 19]