强度模块中的“重试”不适用于生成器

问题描述 投票:1回答:1

我在python3的韧性库中使用“重试”工具时遇到问题。当我使用生成器时,“重试”装饰器似乎不起作用。

我有一个代码示例来说明我的情况:

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)

当引发异常时,它不会重试。有人知道为什么这不起作用吗?

谢谢

python python-3.x generator retry-logic tenacity
1个回答
0
投票

这是具有Tenacity本身的错误/功能/泥潭,其中生成器功能的重试逻辑失败。坚韧的开发人员指出这是因为"generators use exceptions internally."最初的开发人员进一步写道"tenacity.retry() wrapped the generator function, not the generator itself (i.e. the user's code)."基本上没有任何计划可以更改此行为。

为了处理它,应该在调用生成器的方法中添加Tenacity批注-当Tenacity在调用堆栈中冒泡时,可以很容易地捕获异常。

© www.soinside.com 2019 - 2024. All rights reserved.