I have the following code in aws
lambda
to get response from an API until the status is complete. I have used the ThreadPoolExecutor
from concurrent.futures
.
Here is the sample code.
import requests
import json
import concurrent.futures
def copy_url(headers,data):
collectionStatus = 'INITIATED'
retries = 0
print(" The data to be copied is ",data)
while (collectionStatus != 'COMPLETED' or retries <= 50):
r = requests.post(
url=URL,
headers=headers,
data=json.dumps(data))
final_status= r.json().get('status').pop().get('status')
retries += 1
print(" The collection status is",final_status)
with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
future = executor.submit(copy_url,headers,data)
return_value = future.result()
I had already implemented this using regular threads in python. However, since I wanted a return value from the thread tried implementing this. Though this works perfectly in pycharm
, it always throws a timeout error in aws
lambda
.
Could someone please explain why this happens only in aws-lambda
?
Note : I have already tried increasing the lambda
timeout value. This happens only when threadpoolexecutor
is implemented. When I comment out that code it works fine.Also it works fine with the regular python thread implementation