我正在尝试使用坚韧模块来避免来自扩展的频繁请求错误(APIErrors)。我理解坚韧使用装饰器的常见例子,但我想使用坚韧的Retrying()函数,这样我就可以让它重新尝试gspread的电子表格单元格更新方法sheet.update_acell()。
由于某些原因,在sheet.update_acell()中使用重试实际上并不会“给”函数(或其他什么)参数。然而,一个精心设计的多参数示例工作得很好。
我的代码(除了导入和google凭据):
# gspread authorization
gs_client = gspread.authorize(creds)
workbook = gs_client.open_by_key(sheet_key)
sheet = workbook.sheet1
ret = tenacity.Retrying(stop=tenacity.stop_after_attempt(30),
wait=tenacity.wait_exponential(multiplier=0.5, max=60))
# contrived example
def retry_example(arg0, arg1):
print(arg0, arg1)
ret.call(retry_example,'foo','bar') #works perfectly
# gspread example
ret.call(sheet.update_acell(),'A1',1) #doesn't work, the arguments aren't found输出:
foo bar
Traceback (most recent call last):
File "c:/Users/.../tenacitytest.py", line 28, in <module>
ret.call(sheet.update_acell(),'A1',1)
TypeError: update_acell() missing 2 required positional arguments: 'label' and 'value'在没有坚韧的情况下运行扩展程序可以正常工作,因此我确信我调用的update_acell()是正确的。
我觉得这可能是因为update_acell()是一种方法,而不是example_func()?任何帮助都将不胜感激。
发布于 2020-06-06 21:15:02
您不应该在重试调用中使用括号。您应该传递如下参数
(希望这会奏效;)
ret.call(sheet.update_acell,'A1',1)示例:
from tenacity import Retrying, stop_after_attempt
def foo(arg1, arg2):
print('Arguments are: {} {}'.format(arg1, arg2))
raise Exception('Throwing Exception')
def bar(max_attempts=3):
retryer = Retrying(stop=stop_after_attempt(max_attempts), reraise=True)
retryer(foo, 'my arg1', 'my arg2')让我们尝试一下错误场景:
>>> from tenacity import Retrying, stop_after_attempt
>>>
>>> def foo(arg1, arg2):
... print('Arguments are: {} {}'.format(arg1, arg2))
... raise Exception('Throwing Exception')
...
>>> def bar(max_attempts=3):
... retryer = Retrying(stop=stop_after_attempt(max_attempts), reraise=True)
... retryer(foo(), 'my arg1', 'my arg2')
...
>>> bar()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in bar
TypeError: foo() missing 2 required positional arguments: 'arg1' and 'arg2'
>>> https://stackoverflow.com/questions/62237913
复制相似问题