簡體   English   中英

如何在 python 中異步調用 aws 服務 aws lambda

[英]How to call aws service to asynchronously in python aws lambda

我需要在 lambda function 中調用異步 function。執行此操作時,顯示此錯誤

await wasn't used with future

這是我的代碼

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())

這段代碼有什么問題? 我創建了另一個與此相關的問題。 沒有人為我回答,這就是我創建它的原因。

在此getLastModifiedBucketPath方法中,用於獲取特定存儲桶位置最后修改的文件夾名稱。 如果我刪除 asyncio.gather 部分,那么它就可以工作了。 但是我需要在它執行后返回值。

我這周剛開始學習 asyncio,所以對此持保留態度,但我的信念是:

  • loop.run_in_executor返回一個未來
  • 這個未來是沒有等待的; 當你嘗試 splat 它時,你會得到錯誤(在你的 REPL 中嘗試那個片段,你會看到)
  • gather需要 splatted 參數是一個列表,而不是未來

嘗試等待run_in_executor ,然后再將其用作收集參數並查看會發生什么。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM