Python 3.x 在AWS(boto3)呼叫中使用韧性
我在使用boto3调用AWS时遇到间歇性错误。在下面的示例中,由于速率限制,调用instance.all()或i.vpc.tags都可能失败:Python 3.x 在AWS(boto3)呼叫中使用韧性,python-3.x,amazon-web-services,error-handling,boto3,Python 3.x,Amazon Web Services,Error Handling,Boto3,我在使用boto3调用AWS时遇到间歇性错误。在下面的示例中,由于速率限制,调用instance.all()或i.vpc.tags都可能失败: for i in instance.all(): tags = i.vpc.tags 通常情况下,我在自己的函数中使用Tensity作为装饰器,但显然这不能通过调用完成,因为它来自导入的库。如果它不在for循环中,我可以使用重试功能,如下所示: r = tenacity.Retrying( reraise=True, wa
for i in instance.all():
tags = i.vpc.tags
通常情况下,我在自己的函数中使用Tensity作为装饰器,但显然这不能通过调用完成,因为它来自导入的库。如果它不在for循环中,我可以使用重试功能,如下所示:
r = tenacity.Retrying(
reraise=True,
wait=tenacity.wait_random_exponential(multiplier=1, max=60),
stop=tenacity.stop_after_delay(130))
r.call(call_wrapped_in_tenacity())
from botocore.config import Config
config = Config(retries=dict(max_attempts=20))
ec2_client = boto3.client('ec2', config=config)
那么,有没有一种方法可以在不为每个AWS构建新函数的情况下包装这两个对AWS的调用,同时保持重试功能?如果创建自己的函数超出了范围,那么这是我找到的解决此问题的唯一方法。基本上,当您创建boto3客户端时,您会在boto配置中向其传递一个对您自己设置的引用,如下所示:
r = tenacity.Retrying(
reraise=True,
wait=tenacity.wait_random_exponential(multiplier=1, max=60),
stop=tenacity.stop_after_delay(130))
r.call(call_wrapped_in_tenacity())
from botocore.config import Config
config = Config(retries=dict(max_attempts=20))
ec2_client = boto3.client('ec2', config=config)