Lambda + Python +退出代码 [英] Lambda + Python + Exit Code

查看:96
本文介绍了Lambda + Python +退出代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个用Python编写的简单AWS Lambda函数的问题.

I've encountered an issue with a simple AWS Lambda function written in Python.

当我运行Lambda函数时,我的代码按预期运行,结果是正确的,但仍以错误代码(退出代码)结尾:"Process exited before completing request",这导致Lambda运行3次(异步)

When I run my Lambda function, my code is running as expected, the result is correct, but still ends with an error code (exit code): "Process exited before completing request", this is causing the Lambda to run 3 times (async).

您是否有最佳实践来管理Lambda的退出代码?

Do you have any best practice to manage the exit code for Lambda ?

#!/usr/bin/python

import boto3
import sys
import tweepy
import datetime

session = boto3

# Init s3 client
s3 = session.resource('s3')

def get_data_and_push(s3_bucket, s3_key, user):
    # Retrieve CSV file
    try:
        dest = s3.Object(s3_bucket, s3_key)
        dest.download_file(tmpfile)
    except Exception, e:
        print 'An error occured while trying to download CSV file'
        print 'This exception has been thrown :'
        print e
        sys.exit(1)

    # Authenticate to Twitter
    try:
        auth = tweepy.OAuthHandler(t_consumer_key, t_consumer_secret)
        auth.set_access_token(t_access_token_key, t_access_token_secret)
        api = tweepy.API(auth)
    except Exception, e:
        print 'Cannot authenticate to Twitter.'
        print 'This exception has been thrown :'
        print e
        sys.exit(2)

    data = api.get_user(user)
    print 'User : ' + data.screen_name
    print 'Followers : ' + str(data.followers_count)
    print 'Friends : ' + str(data.friends_count)
    print '-----------'

    # Get today's date
    today = datetime.datetime.now().strftime("%Y-%m-%d")

    # Write result
    try:
        # Write result at the end of the file
        file = open(tmpfile, 'a')
        file.write(today + ',' + str(data.followers_count) + ',' + str(data.friends_count)+ '\n')
        file.close()
    except Exception, e:
        print 'Unable to write in temp file'
        print 'This exception has been thrown :'
        print e
        sys.exit(5)

    # Upload final file
    try:
        # Push file to S3
        dest.upload_file(tmpfile)
    except Exception, e:
        print 'An error occured while trying to upload CSV file'
        print 'This exception has been thrown :'
        print e
        sys.exit(6)

def main(event, context):
    for user in userlist:
        get_data_and_push(bucket, 'export_' + user + '.csv', user)
    sys.exit(0)

感谢您的帮助,

推荐答案

是的,删除代码末尾的sys.exit(0),应该这样做:-)

Short

Yes, remove the sys.exit(0) at the end of your code, that should do it :-)

通过执行sys.exit(0),您实际上停止了在Lambda函数中运行代码的进程.这不是执行者所期望的.

By doing sys.exit(0) you actually stop the process running your code in the Lambda Function. And this is not expected by the executor.

我假设Lambda函数的处理程序实际上是在AWS框架内运行的.因此,您已经处于python进程中,并且您的处理程序在AWS代码中的某个地方被调用.因此,如果退出该流程,则实际上是简化了AWS的框架,该框架无法处理Lambda执行的解析.

I assume the handler of a Lambda Function is actually run from within AWS's framework. Hence you already are in a python process, and your handler is called somewhere in AWS's code. So if you exit the process, you actually short-cut AWS's framework, which cannot handle the resolution of the Lambda's execution.

这篇关于Lambda + Python +退出代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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