[英]How to call aws service to asynchronously in python aws lambda
I need to call to async function inside lambda function. When executing this, it's showing this error我需要在 lambda function 中调用异步 function。执行此操作时,显示此错误
await wasn't used with future
Here is my code这是我的代码
async def main():
bucketName = 'data-store-test'
folder = 'Contacts-Aggre'
lastModifiedFolderPath = await getLastModifiedBucketPath(bucketName,folder)
print('Received lastModifiedFolderPath:',lastModifiedFolderPath)
async def getLastModifiedBucketPath(bucketName, prefix):
loop = asyncio.get_running_loop()
bucket_objects = await asyncio.gather(*loop.run_in_executor(None, functools.partial(s3_client.list_objects_v2, Bucket=bucketName, Prefix=prefix)))
all = bucket_objects['Contents']
latest = max(all, key=lambda x: x['LastModified'])
folderPaths = latest['Key'].split('/')
lastModifiedFolder = folderPaths[1] if len(folderPaths) >=2 else folderPaths[0]
return lastModifiedFolder
def lambda_handler(event, context):
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
What is the issue in this code?这段代码有什么问题? I created another question also related to this.
我创建了另一个与此相关的问题。 No one answered for me, that's why I created this.
没有人为我回答,这就是我创建它的原因。
In this getLastModifiedBucketPath method used to get specific bucket location last modified folder name.在此getLastModifiedBucketPath方法中,用于获取特定存储桶位置最后修改的文件夹名称。 If I remove asyncio.gather part, then it's working.
如果我删除 asyncio.gather 部分,那么它就可以工作了。 But I need to return value after it execute.
但是我需要在它执行后返回值。
I'm just learning asyncio this week, so take this with a grain of salt, but my belief is:我这周刚开始学习 asyncio,所以对此持保留态度,但我的信念是:
loop.run_in_executor
returns a future loop.run_in_executor
返回一个未来gather
needs the splatted argument to be a list, not a future gather
需要 splatted 参数是一个列表,而不是未来Try awaiting that run_in_executor
before using it as an argument to gather and see what happens.尝试等待
run_in_executor
,然后再将其用作收集参数并查看会发生什么。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.