如何从Python向同一个长期运行的AWS Lambda函数发出多个HTTP RequestResponse请求?

时间:2019-03-09 16:09:12

标签: python amazon-web-services aws-lambda

我了解我可以同步调用AWS Lambda函数并读取结果:

data = invoke_response['Payload'].read()

如何继续连接到相同的长时间运行的Lambda函数调用以获取部分结果,直到完成?

1 个答案:

答案 0 :(得分:0)

简而言之,您不能在示例中使用boto3 lambda客户端。但是,您可以做的是监视CloudWatch日志。

1)将您希望看到的部分结果发布到记录器中的lambda函数中。

import logging
logger = logging.getLogger()
logger.setLevel(logging.INFO)
def my_logging_handler(event, context):
    results = "something half done"
    logger.info('MY PARTIAL RESULTS'.format(results))
    logger.error('something went wrong')
    return 'Hello from Lambda!'  

2)异步启动lambda

response = client.invoke(
    FunctionName='string',
    InvocationType='Event' # To invoke asynchronously InvocationType must be Event,
    LogType='None'|'Tail',
    ClientContext='string',
    Payload=b'bytes'|file,
    Qualifier='string'
)

3)创建一个循环,该循环在指定的时间间隔内为与您的lambda关联的日志流调用GetLogEvents

Request Syntax:
{
   "endTime": number,
   "limit": number,
   "logGroupName": "string",
   "logStreamName": "string",
   "nextToken": "string",
   "startFromHead": boolean,
   "startTime": number
}

4)从日志流响应中提取部分结果

Response Syntax:
{
   "events": [ 
      { 
         "ingestionTime": number,
         "message": "string", # The partial response you posted will be seen here
         "timestamp": number
      }
   ],
   "nextBackwardToken": "string",
   "nextForwardToken": "string"
}