Created
July 29, 2017 09:47
-
-
Save hidesakai/f1a68c0c6761313d1953a5374351c39e to your computer and use it in GitHub Desktop.
[Python]AWS Lambdaでのリトライ処理(Exponential Backoff)のメモ ref: http://qiita.com/hidesakai/items/bb529c4c29952c41c218
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from chalice import Chalice | |
from datetime import datetime | |
from retrying import retry | |
import boto3 | |
from botocore.exceptions import ClientError | |
app = Chalice(app_name='exponential-backoff') | |
def retryIfClientError(exception): | |
print(datetime.now().strftime("%Y/%m/%d %H:%M:%S")) | |
print('retrying...') | |
# Falseを返すまで再試行をする | |
return isinstance(exception, ClientError) | |
# retry_on_exception: 特定のException拾うため | |
# stop_max_attempt_number: 試行回数 | |
# wait_exponential_multiplier: 乗数 | |
@retry(retry_on_exception=retryIfClientError, stop_max_attempt_number=3, wait_exponential_multiplier=1000) | |
def retryS3Test(): | |
s3 = boto3.resource('s3') | |
client = s3.meta.client | |
response = client.get_object(Bucket='mybucket', Key='存在しないかもしれないファイル') | |
@app.route('/') | |
def index(): | |
retryS3Test() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ pip install retrying |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
retrying==1.3.3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment