如何在AWS Lambda Function中拥有多个处理程序? [英] How to have more than one handler in AWS Lambda Function?

查看:225
本文介绍了如何在AWS Lambda Function中拥有多个处理程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常大的python文件,其中包含多个已定义的函数.如果您熟悉AWS Lambda,则在创建lambda函数时,可以指定处理程序,该处理程序是服务执行我的代码时AWS Lambda可以调用的代码中的函数,下面在my_handler.py文件中表示:/p>

I have a very large python file that consists of multiple defined functions. If you're familiar with AWS Lambda, when you create a lambda function, you specify a handler, which is a function in the code that AWS Lambda can invoke when service executes my code, which is represented below in my_handler.py file:

    def handler_name(event, context):
    ...
    return some_value

链接源: https://docs.aws.amazon.com/lambda/latest/dg/python-programming-model-handler-types.html

但是,如上所述,我在my_handler.py中有多个定义的函数,它们具有自己的事件和上下文.因此,这将导致错误.在python3.6中有什么方法可以解决这个问题?

However, as I mentioned above, I have multiple defined functions in my_handler.py that have their own events and contexts. Therefore, this will result in an error. Are there any ways around this in python3.6?

推荐答案

您的单个​​处理程序函数将需要负责解析传入的事件,并确定采取的适当路线.例如,假设您的其他函数称为helper1helper2.您的Lambda处理函数将检查传入的事件,然后根据传入事件中的字段之一(即,我们将其称为EventType)调用helper1helper2,同时传入事件和上下文对象.

Your single handler function will need to be responsible for parsing the incoming event, and determining the appropriate route to take. For example, let's say your other functions are called helper1 and helper2. Your Lambda handler function will inspect the incoming event and then, based on one of the fields in the incoming event (ie. let's call it EventType), call either helper1 or helper2, passing in both the event and context objects.

def handler_name(event, context):
  if event['EventType'] = 'helper1':
    helper1(event, context)
  elif event['EventType'] = 'helper2':
    helper2(event, context)

def helper1(event, context):
  pass

def helper2(event, context):
  pass

这只是伪代码,我自己还没有对其进行测试,但是应该可以理解这个概念.

This is only pseudo-code, and I haven't tested it myself, but it should get the concept across.

这篇关于如何在AWS Lambda Function中拥有多个处理程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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