Issue
I'm trying to do this using the asyncio library but I don't really understand how it works. Shouldn't it just be as simple as calling the async function in the handler without an await in front of it? e.g.
def my_handler(event, context):
someAsyncFunction()
return random_request_id
Please note that I don't want to wait for someAsyncFunction
to return
Solution
AWS Lambda doesn't work the way you are trying to use it. As soon as you call return
in the handler of the Lambda function, the entire function runtime will be stopped by AWS. It isn't possible to return a response from a Lambda invocation while that same Lambda invocation is still performing any sort of processing task in the background.
What you can do is create two Lambda functions:
Function 1:
- Takes a
random_request_id
as input, and does some longer running processing and exits.
Function 2:
- Creates a random_request_id, calls Function 1 with invocation type Event so it doesn't wait on that Lambda function to complete, and returns the
random_request_id
.
You could also consider placing an SQS queue between the two Lambda functions to decouple them.
This kind of multiple Lambda function workflow can quickly become unwieldy, so I recommend looking into AWS Step Functions to orchestrate this workflow.
Answered By - Mark B
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.