【发布时间】:2014-06-03 21:09:56
【问题描述】:
我目前正在使用 Google 的 BigQuery API,当调用它时,偶尔会给我:
apiclient.errors.HttpError: <HttpError 500 when requesting https://www.googleapis.com/bigquery/v2/projects/some_job?alt=json returned "Unexpected. Please try again.">
返回是一件愚蠢的事情,但无论如何,当我为任何调用的方法得到这个时,我只想睡一两秒钟,然后再试一次。基本上,我想用类似的东西包装每个方法:
def new_method
try:
method()
except apiclient.errors.HttpError, e:
if e.resp.status == 500:
sleep(2)
new_method()
else:
raise e
有什么好的方法吗?
我不想显式地重新定义类中的每个方法。我只想对类中的每个方法自动应用一些东西,所以我会为以后的事情做准备。理想情况下,我会采用一个类对象 o,并围绕它创建一个包装器,该包装器使用此尝试重新定义类中的每个方法,但包装器除外,因此我得到了一些新对象 p,它在收到 500 错误时自动重试。
【问题讨论】:
-
装饰师是我的小费
-
你真的希望这可能永远递归吗?
标签: python