Lambda + Python + Exit Code
我遇到了一个用Python编写的简单的AWS lambda函数的问题。
当我运行lambda函数时,我的代码按预期运行,结果是正确的,但仍然以错误代码(退出代码)结尾:
您是否有任何管理lambda退出代码的最佳实践?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | #!/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)+ ' ') 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) |
谢谢你的帮助,
短
是的,删除代码末尾的
通过执行
我假设lambda函数的处理程序实际上是从AWS的框架中运行的。因此,您已经在一个Python进程中,并且您的处理程序在AWS的代码中的某个地方被调用。因此,如果退出进程,实际上是缩短了AWS的框架,它无法处理lambda执行的解析。