使用async / await从python异步调用AWS Lambda函数

时间:2019-10-02 13:26:09

标签: python python-3.x asynchronous aws-lambda

我有一个AWS lambda函数,需要以异步方式(激发并忘记)调用该函数,并以非阻塞方式将其返回。

loop = asyncio.get_event_loop()

async def f(payload):
    lambda_client = boto3.client('lambda')
    response = lambda_client.invoke(
    FunctionName='FUNC_NAME',
    InvocationType='RequestResponse',
    LogType='Tail',
    Payload=payload,
    Qualifier='$LATEST'
    )
    response_body = response['Payload']
    response_str = response_body.read().decode('utf-8')
    response_dict = eval(response_str)
    return response_dict


async def g():

    payload = json.dumps({
      "test_bucket": "MY_BUCKET",
      "test_key": "my_test_key.csv",
      "testpred_bucket": "MY_BUCKET",
      "testpred_key": "my_test_key_new.csv",
      "problem": "APROBLEM"
    })
    # Pause here and come back to g() when f() is ready
    r = await f(payload)
    print(r)

这可行,但这实际上并没有达到fire and forget的目的。我了解需要使用asyncio.ensure_future的方式,但是如果执行asyncio.ensure_future(f(payload)),如何捕获函数f的返回值。我是python异步的新手,目前尚不清楚。

有人可以建议吗?

2 个答案:

答案 0 :(得分:0)

  • 这是我在aws lambda中所做的事情,我想执行4个发布请求,然后收集所有响应。
    loop = asyncio.get_event_loop()

    if loop.is_closed():
        loop = asyncio.new_event_loop()

    #The perform_traces method i do all the post method
    task = loop.create_task(perform_traces(payloads, message, contact_centre))
    unique_match, error = loop.run_until_complete(task)
    loop.close()

perform_trace方法中,这就是我使用会话等待的方式

    future_dds_responses = []

    async with aiohttp.ClientSession() as session:

        for payload in payloads:
            future_dds_responses.append(dds_async_trace(session, payload, contact_centre))

        dds_responses, pending = await asyncio.wait(future_dds_responses)

dds_async_trace中,这就是我使用aiohttp.ClientSession会话完成帖子的方式

        async with session.post(pds_url,
                                data=populated_template_payload,
                                headers=PDS_HEADERS,
                                ssl=ssl_context) as response:
            status_code = response.status

答案 1 :(得分:0)

我能够用asyncio.ensure_future

解决此问题
import boto3
import json
from flask import jsonify
import signal
import sys
import asyncio
import aiohttp
import json


def run():
    loop = asyncio.get_event_loop()
    loop.run_until_complete(run_job())


async def run_job():
    asyncio.ensure_future(launch_lambda())  # fire and forget async_foo()
    print('waiting for future ...')

async def launch_lambda():

    payload = json.dumps({
      "test_bucket": "my_bucket",
      "test_key": "my_test_key",
      "testpred_bucket": "my_bucket",
      "testpred_key": "my_pred_key",
      "problem": "APROBLEM"
    })
    result = await get_lambda_response(payload)
    print(result)

async def get_lambda_response(payload):
        lambda_client = boto3.client('lambda')
        response = lambda_client.invoke(
        FunctionName='FUNC_NAME',
        InvocationType='RequestResponse',
        LogType='Tail',
        Payload=payload,
        Qualifier='$LATEST'
        )
        response_body = response['Payload']
        response_str = response_body.read().decode('utf-8')
        response_dict = eval(response_str)
        return response_dict


run()