简体   繁体   English

具有多种功能的AWS Python Lambda

[英]AWS Python Lambda with multiple functions

I have an AWS Lambda function in python3.7. 我在python3.7中有一个AWS Lambda函数。 The way its set up im running the lambda_handler(event, context) function and passing data to a separate function that calls itself multiple times depending on what is passed into it. 它的设置方式是运行lambda_handler(event, context)函数并将数据传递到一个单独的函数,该函数根据传递给它的内容多次调用自身。 How do I then return data from the second function? 然后如何从第二个函数返回数据?

import json
import boto3

def lambda_handler(event, context):
    # code to get initial data
    x = second_function(data)
    print(x)
    return x



def second_function(data):
    # code to manipulate data
    if condition:
       print(newData)
       second_function(newData)
    else:
       return allData

I expected this to return allData back through the lambda_handler function, but instead returns null 我期望这可以通过lambda_handler函数返回allData,但是返回null

And logged is 并记录为

newData
newData
newData
None

I am using the second function to get data based on the last PaginationToken . 我正在使用第二个函数来基于最后一个PaginationToken获取数据。 Is there a better way to get paginated data rather than creating a second recursive function? 有没有比创建第二个递归函数更好的方法来获取分页数据?

One option is to use a boto3 paginator . 一种选择是使用boto3分页器

Alternatively, you could use a loop rather than a recursive function . 或者,您可以使用循环而不是递归函数

It would be something like: 就像这样:

response = api_call()
<do stuff with response>
while response['NextToken']:
    response=api_call(NextToken=response['NextToken'])
    <do stuff with response>

You can probably avoid having to double-up the <do stuff> bit by improving on the while statement. 您可能可以通过改进while语句来避免将<do stuff>位加倍。

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

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