Skip to content

Instantly share code, notes, and snippets.

@hidesakai
Created July 29, 2017 09:47
Show Gist options
  • Save hidesakai/f1a68c0c6761313d1953a5374351c39e to your computer and use it in GitHub Desktop.
Save hidesakai/f1a68c0c6761313d1953a5374351c39e to your computer and use it in GitHub Desktop.
[Python]AWS Lambdaでのリトライ処理(Exponential Backoff)のメモ ref: http://qiita.com/hidesakai/items/bb529c4c29952c41c218
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()
$ pip install retrying
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment