There is a difference between an 'async AWS lambda invocation' and 'async python code'. When you set the InvocationType
to 'Event'
, by definition, it does not ever send back a response.
In your example, invoke()
immediately returns None
, and does not implicitly start up anything in the background to change that value at a later time (thank goodness!). So, when you look at the value of response
15 seconds later, it's still None
.
It seems what you really want is the RequestResponse
invocation type, with asynchronous Python code. You have a bunch of options to choose from, but my favorite is concurrent.futures
. Another is threading
.
Here's an example using concurrent.futures
:
(If you're using Python2 you'll need to pip install futures
)
from concurrent.futures import ThreadPoolExecutor
import json
payload = {...}
with ThreadPoolExecutor(max_workers=5) as executor:
futs = []
for x in xrange(0, 5):
futs.append(
executor.submit(client.invoke,
FunctionName = "loadSpotsAroundPoint",
InvocationType = "RequestResponse",
Payload = bytes(json.dumps(payload))
)
)
results = [ fut.result() for fut in futs ]
print results
Another pattern you might want to look into is to use the Event
invocation type, and have your Lambda function push messages to SNS, which are then consumed by another Lambda function. You can check out a tutorial for SNS-triggered lambda functions here.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…