英文:
How to send response from AWS lambda to client and continue processing code
问题
我正在AWS Lambda上运行一段Python代码。我是通过Postman触发代码的。一旦触发了Lambda脚本,它可以运行大约5分钟。因此,客户端必须等待响应那么长时间。相反,我希望我的Lambda脚本能够读取请求主体,处理响应主体并发送响应,同时继续运行脚本的其余部分。
我该如何实现这一点?通常在基于服务器的应用程序中,如何实现这种用例?
我考虑将代码的其余部分作为另一个Lambda函数,并从该函数中使用requests库触发它。一旦触发,我打算将响应发送给客户端,但即使我触发另一个函数,它仍然会等待响应。
英文:
I am running a piece of Python code on AWS Lambda. I am triggering the code through Postman. The lambda script once triggered can run for around 5mins. So the client has to wait for a response for that time. Instead I want my lambda script to read the request body, process the response body and send the response and still keep running the remaining part of the script.
How do I achieve this? How is this use case achieved usually in a server based application?
I thought of having the remaining part of the code as another Lambda function and triggering it from this function using the requests library. Once triggered I thought of sending the response to the client but the even if I trigger another function it is still going to wait for a response.
答案1
得分: 1
你可以调用第二个Lambda函数Event
调用类型,这将允许你的第一个函数返回响应给客户端,而另一个在后台运行。
或者,你可能会受益于使用AWS Step Functions来处理异步部分,因为它可以使其更具弹性和可观察性。
如果你想在这里做一些更丰富的事情,这是一篇有趣的博客文章,展示了如何将异步服务器端处理与客户端的可见性配对。
https://aws.amazon.com/blogs/compute/implementing-reactive-progress-tracking-for-aws-step-functions/
英文:
You can invoke the second Lambda function Event
Invocation Type, which will allow your first function to return a response to your client while the other runs in the background.
Alternatively, you might benefit from using AWS Step Functions for the asynchronous part, as you can make it more resilient and observable.
And if you are looking to do something richer here, this is an interesting blog post to show how you can pair asynchronous server side processing with visibility to the client side.
https://aws.amazon.com/blogs/compute/implementing-reactive-progress-tracking-for-aws-step-functions/
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论