我试图将所有函数包装在库的实例中,以重试 500 个错误(包装以避免强制团队成员在每个函数上专门添加重试代码)。我以前做过类似的事情,但对于 BigQuery,我没有运气。这是我的代码:
def bq_methods_retry(func):
num_retries = 5
@functools.wraps(func)
def wrapper(*a, **kw):
sleep_interval = 2
for i in xrange(num_retries):
try:
return func(*a, **kw)
except apiclient.errors.HttpError, e:
if e.resp.status == 500 and i < num_retries-1:
logger.info("got a 500. retrying.")
time.sleep(sleep_interval)
sleep_interval = min(2*sleep_interval, 60)
else:
logger.info('failed with unexpected apiclient error:')
raise e
except:
logger.info('failed with unexpected error:')
raise
return wrapper
def decorate_all_bq_methods(instance, decorator):
for k, f in instance.__dict__.items():
if inspect.ismethod(f):
name = f.func_name
setattr(instance, k, decorator(f))
return instance
...
service = discovery.build('bigquery', 'v2', http=http)
#make all the methods in the service retry when appropriate
service = decorate_all_bq_methods(service, bq_methods_retry)
jobs = decorate_all_bq_methods(service.jobs(), bq_methods_retry)
然后,当我运行类似的命令时:
jobs.query(projectId=some_id, body=some_query).execute()
500 错误永远不会被 bq_methods_retry 捕获,而是传递给程序的其余部分。
有什么想法吗?我也愿意接受更好的重试解决方案。
最佳答案
bq 命令行工具使用的 BigQuery 客户端通过包装 HTTP 对象来执行类似的操作。它不会重试,但会转换异常,因此您可能会使用相同类型的 Hook 。
请注意,您可能需要谨慎重试某些类型的操作;例如,如果您重试附加数据的作业插入,如果返回响应时遇到网络错误,则原始请求实际上可能会成功,因此您将插入相同的数据两次。为了避免这种情况,您可以传入自己的作业 ID,这应该可以防止它运行两次(因为第二次作业已经存在)。
查看代码here .
关于python - 包装库函数以重试 500 错误无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25348782/