【问题标题】:"Retry" from tenacity module doesn't work with a generator坚韧模块的“重试”不适用于生成器
【发布时间】:2020-07-05 09:33:19
【问题描述】:

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

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

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 python-tenacity


    【解决方案1】:

    这是 Tenacity 本身的错误/功能/泥潭,其中重试逻辑在生成器函数上失败。 Tenacity 开发人员表示这是因为 "generators use exceptions internally." 最初的开发人员进一步写道 "tenacity.retry() wrapped the generator function, not the generator itself (i.e. the user's code)." 基本上没有任何计划来解决这种行为,如果它甚至可以完成的话。

    为了处理它,应该将 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
    ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-15
      • 1970-01-01
      相关资源
      最近更新 更多