首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python3用坚韧重试(w/out装饰器)在使用扩展时出现声称“缺少参数”的错误

Python3用坚韧重试(w/out装饰器)在使用扩展时出现声称“缺少参数”的错误
EN

Stack Overflow用户
提问于 2020-06-06 20:52:28
回答 1查看 1.4K关注 0票数 1

我正在尝试使用坚韧模块来避免来自扩展的频繁请求错误(APIErrors)。我理解坚韧使用装饰器的常见例子,但我想使用坚韧的Retrying()函数,这样我就可以让它重新尝试gspread的电子表格单元格更新方法sheet.update_acell()

由于某些原因,在sheet.update_acell()中使用重试实际上并不会“给”函数(或其他什么)参数。然而,一个精心设计的多参数示例工作得很好。

我的代码(除了导入和google凭据):

代码语言:javascript
复制
# 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

输出:

代码语言:javascript
复制
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()?任何帮助都将不胜感激。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-06 21:15:02

您不应该在重试调用中使用括号。您应该传递如下参数

(希望这会奏效;)

代码语言:javascript
复制
ret.call(sheet.update_acell,'A1',1)

示例:

代码语言:javascript
复制
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')

让我们尝试一下错误场景:

代码语言:javascript
复制
>>> 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'
>>>   
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62237913

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档