所以我使用 boto 来调用我的 lambda 函数并测试我的后端.我想异步调用它们.我注意到invoke_async"已被弃用,不应使用.相反,您应该使用具有事件"的 InvocationType 的invoke"来异步执行该功能.
我似乎无法弄清楚如何在函数返回时从函数中获取响应.我尝试了以下方法:
payload3=b"""{纬度":39.5732160891,经度":-119.672918997,半径":100}"""客户端 = boto3.client('lambda')对于范围 (0, 5) 中的 x:响应 = client.invoke(FunctionName="loadSpotsAroundPoint",调用类型='事件',有效载荷=有效载荷3)时间.睡眠(15)打印(json.loads(响应['Payload'].read()))打印( ")即使我告诉代码休眠 15 秒,当我尝试打印它时,响应变量仍然是空的.如果我将 invokation InvokationType 更改为RequestResponse",它一切正常并且响应变量打印,但这是同步的.我错过了一些简单的东西吗?当异步调用返回时,我如何执行一些代码,例如打印结果?
谢谢.
解决方案异步执行的 AWS Lambda 函数不返回执行结果.如果异步调用请求成功(即没有由于权限等导致的错误),AWS Lambda 会立即返回 HTTP 状态代码 202接受,并且不承担传达有关此异步调用结果的任何信息的进一步责任.
来自 AWS Lambda 调用操作的文档:
响应语法HTTP/1.1 状态码X-Amz-Function-Error:FunctionErrorX-Amz-Log-Result: LogResult有效载荷响应元素
如果动作成功,服务发回以下HTTP回应.
状态代码成功请求的 HTTP 状态代码将在 200 范围内.对于 RequestResponse 调用类型,此状态代码将为 200.对于 Event 调用类型,此状态代码将为 202.对于 DryRun 调用类型,状态代码将为 204.
[...]
响应返回以下内容作为 HTTP 正文.
有效载荷是Lambda返回的对象的JSON表示功能.仅当调用类型为RequestResponse.
SO I'm using boto to invoke my lambda functions and test my backend. I want to invoke them asynchronously. I have noted that "invoke_async" is deprecated and should not be used. Instead you should use "invoke" with an InvocationType of "Event" to do the function asynchronously.
I can't seem to figure out how to get the responses from the functions when they return though. I have tried the following:
payload3=b"""{ "latitude": 39.5732160891, "longitude": -119.672918997, "radius": 100 }""" client = boto3.client('lambda') for x in range (0, 5): response = client.invoke( FunctionName="loadSpotsAroundPoint", InvocationType='Event', Payload=payload3 ) time.sleep(15) print(json.loads(response['Payload'].read())) print(" ")Even though I tell the code to sleep for 15 seconds, the response variable is still empty when I try and print it. If I change the invokation InvokationType to "RequestResponse" it all works fine and response variable prints, but this is synchronous. Am I missing something easy? How do i execute some code, for example print out the result, when the async invokation returns??
Thanks.
解决方案An asynchronously executed AWS Lambda function doesn't return the result of execution. If an asynchronous invocation request is successful (i.e. there were no errors due to permissions, etc), AWS Lambda immediately returns the HTTP status code 202 ACCEPTED and bears no further responsibility for communicating any information about the outcome of this asynchronous invocation.
From the documentation of AWS Lambda Invoke action:
Response Syntax HTTP/1.1 StatusCode X-Amz-Function-Error: FunctionError X-Amz-Log-Result: LogResult PayloadResponse Elements
If the action is successful, the service sends back the following HTTP response.
StatusCodeThe HTTP status code will be in the 200 range for successful request. For the RequestResponse invocation type this status code will be 200. For the Event invocation type this status code will be 202. For the DryRun invocation type the status code will be 204.
[...]
The response returns the following as the HTTP body.
PayloadIt is the JSON representation of the object returned by the Lambda function. This is present only if the invocation type is RequestResponse.
更多推荐
使用 boto 调用 lambda 函数如何异步执行此操作?
发布评论