我遇到了一個用 Python 編寫的簡單 AWS Lambda 函數(shù)的問題。當我運行我的 Lambda 函數(shù)時,我的代碼按預期運行,結(jié)果是正確的,但仍然以錯誤代碼(退出代碼): 結(jié)束"Process exited before completing request",這導致 Lambda 運行 3 次(異步)。您是否有任何最佳實踐來管理 Lambda 的退出代碼?#!/usr/bin/pythonimport boto3import sysimport tweepyimport datetimesession = boto3# Init s3 clients3 = 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)謝謝你的幫助,
1 回答

慕無忌1623718
TA貢獻1744條經(jīng)驗 獲得超4個贊
短的
是的,刪除sys.exit(0)
代碼末尾的 ,應該這樣做:-)
更長
通過這樣做,sys.exit(0)
您實際上會停止在 Lambda 函數(shù)中運行您的代碼的進程。這不是執(zhí)行者所期望的。
我假設 Lambda 函數(shù)的處理程序?qū)嶋H上是在 AWS 的框架內(nèi)運行的。因此,您已經(jīng)在一個 python 進程中,并且您的處理程序在 AWS 代碼中的某處被調(diào)用。所以如果你退出這個過程,你實際上是在走捷徑AWS的框架,它無法處理Lambda執(zhí)行的解析。
添加回答
舉報
0/150
提交
取消