当我使用生成器时,
@retry
装饰器似乎不起作用。
我有一个代码示例来说明我的情况:
from tenacity import retry, wait_exponential
@retry(wait=wait_exponential(multiplier=1, min=1, max=1))
def test_retry():
print("test retry from tenacity")
for i in range(10):
if i == 0: raise Exception
yield i
def gen():
yield from test_retry()
bar = gen()
for foo in bar:
print(foo)
当它引发异常时,它不会重试。
有人知道为什么这不起作用吗?
这是 Tenacity 本身的一个错误/功能/泥沼,其中重试逻辑在生成器函数上失败。 Tenacity 开发人员表示,这是因为 “生成器在内部使用异常。” 原始开发人员进一步写道 “tenacity.retry() 包装了生成器函数,而不是生成器本身(即用户的代码)。”基本上,似乎没有任何计划来解决这种行为(如果可以的话)。
为了解决这个问题,应该将 Tenacity 注释添加到调用生成器的方法中 - 当异常通过调用堆栈向上冒泡时,Tenacity 可以轻松捕获异常。 重要提示:生成器函数也不得隐藏异常。
# in generator do this and add retry annotations to calling method
...
try:
do_something()
except Exception as ex:
log_or_do_something_else()
raise
finally:
cleanup()
yield something
...
# in generator don't do this
...
try:
do_something()
except Exception as ex:
pass
yield something
...