简体   繁体   English

在 Python 中制作异步 AWS Lambda Function

[英]Make Async AWS Lambda Function in Python

I'm looking at making a fire and forget function call within aws lambda.我正在考虑生火并忘记在aws lambda中调用function。

I'm using FASTAPI我正在使用 FASTAPI

I've done the following:我做了以下事情:

from fastapi import APIRouter, BackgroundTasks

router = APIRouter()

@router.post('/post_trigger_data')
async def post_trigger_data(trigger_data: TriggerData, background_tasks: BackgroundTasks):

    data = {
        'uuid': trigger_data.uuid,
        'status': 'Initializing',
        'status_message': '',
        'form_body': trigger_data.data
    }

    utils.post_data_to_dynamo_db('TriggerProcessingTableName', data)

    background_tasks.add_task(process_trigger_data, trigger_data.uuid) # This is where it should fire and forget

    return response


def process_trigger_data(uuid: str):

    time.sleep(10)
    data = {'data': 'RUN 1', 'uuid': uuid, 'status': 'Pending'}
    utils.post_data_to_dynamo_db('TriggerProcessingTableName', data)

    time.sleep(10)
    data = {'data': 'RUN 2', 'uuid': uuid, 'status': 'OK'}
    utils.post_data_to_dynamo_db('TriggerProcessingTableName', data)

    return data

I'm expecting the process_trigger_data function to be executed as a fire and forget but what is occurring is that my lambda function is waiting for the process_trigger_data to execute fully before "return response".我期待 process_trigger_data function 作为火灾执行并忘记,但发生的是我的 lambda function 在“完全等待响应之前执行”

How should I do to make a fire and forget function call?我该怎么做才能生火并忘记 function 调用? I've tried creating an api function of process_trigger_data so that it should run another aws instance but I get the same result, it waits for the completion before returning response我尝试创建一个 api function 的 process_trigger_data 以便它应该运行另一个 aws 实例但我得到相同的结果,它在返回响应之前等待完成

How should I do this?我该怎么做?

Thanks谢谢

So when you execute a task in Lambda it'll wait until all processes have finished before executing/or time out.因此,当您在 Lambda 中执行任务时,它会等到所有进程完成后再执行/或超时。

There are two ways to approach this.有两种方法可以解决这个问题。

1) You can use SNS to post a message with a payload that another Lamdba is subscribed to. 1) 您可以使用 SNS 发布带有另一个 lamdba 订阅的有效负载的消息。

The pitfall to this is that if you function gets very busy you're Lambda's can fan out by default to a very high number - if you have DB connections from these Lambda's then it can max out your connection pool.这样做的缺陷是,如果您的 function 变得非常忙碌,那么您的 Lambda 可以默认扇出一个非常高的数量 - 如果您有来自这些 Lambda 的数据库连接,那么它可以最大化您的连接池。

2) If there's likely to be a lot of these tasks SQS is a better option since your consuming Lambda(s) will just process the messages as fast as possible without the big fan out. 2) 如果可能有很多这样的任务,SQS 是一个更好的选择,因为您使用 Lambda(s) 只会尽可能快地处理消息,而不会产生大的扇出。

SQS is what I'd recommend: https://docs.aws.amazon.com/lambda/latest/dg/with-sqs.html SQS 是我推荐的: https://docs.aws.amazon.com/lambda/latest/dg/with-sqs.html

SNS Docs are here: https://docs.aws.amazon.com/lambda/latest/dg/with-sns.html SNS 文档在这里: https://docs.aws.amazon.com/lambda/latest/dg/with-sns.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM