使用boto调用lambda函数如何异步执行此操作? [英] Using boto to invoke lambda functions how do I do so asynchronously?

查看:159
本文介绍了使用boto调用lambda函数如何异步执行此操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在使用boto调用我的lambda函数并测试我的后端.我想异步调用它们.我注意到"invoke_async"已被弃用,不应使用.相反,您应该将InvocationType和"Event"的InvocationType一起使用,以异步执行该功能.

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("\n")

即使我告诉代码休眠15秒钟,当我尝试打印它时,响应变量仍然为空.如果我将Invokation InvokationType更改为"RequestResponse",则一切正常,并且可以打印响应变量,但这是同步的.我想念一些容易的事吗?异步调用返回时,如何执行一些代码,例如打印结果?

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??

谢谢.

推荐答案

异步执行的AWS Lambda函数不会返回执行结果.如果异步调用请求成功(即,没有由于权限等导致的错误),AWS Lambda会立即返回HTTP状态代码 202接受,并且不承担与传递有关此异步调用结果的任何信息的进一步责任.

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.

AWS Lambda调用操作的文档中:

响应语法

HTTP/1.1 StatusCode
X-Amz-Function-Error: FunctionError
X-Amz-Log-Result: LogResult

Payload

响应元素

如果操作成功,该服务将发回以下HTTP 响应.

Response Elements

If the action is successful, the service sends back the following HTTP response.

用于成功请求的HTTP状态代码将在200范围内. 对于RequestResponse调用类型,此状态码将为200. 对于Event调用类型,此状态码将为202 .对于DryRun调用类型,状态码将为204.

The 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.

[...]

响应返回以下内容作为HTTP正文.

The response returns the following as the HTTP body.

它是Lambda返回的对象的JSON表示形式 功能. 仅当调用类型为 RequestResponse.

It is the JSON representation of the object returned by the Lambda function. This is present only if the invocation type is RequestResponse.

这篇关于使用boto调用lambda函数如何异步执行此操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆