【问题标题】:Python retry using tenacity without decoratorPython在没有装饰器的情况下使用坚韧重试
【发布时间】:2018-11-24 19:13:25
【问题描述】:

我正在尝试使用坚韧(没有装饰器)重试。我的代码看起来像 here 解释的那样。

import logging
from tenacity import retry
import tenacity


def print_msg():
    try:
        logging.info('Hello')
        logging.info("World")
        raise Exception('Test error')
    except Exception as e:
        logging.error('caught error')
        raise e


if __name__ == '__main__':
    logging.basicConfig(
        format='%(asctime)s,%(msecs)d %(levelname)-8s [%(filename)s:%(lineno)d] %(message)s',
        datefmt='%d-%m-%Y:%H:%M:%S',
        level=logging.INFO)
    logging.info('Starting')
    try:
        r = tenacity.Retrying(
            tenacity.stop_after_attempt(2),
            tenacity.wait_incrementing(start=10, increment=100, max=1000),
            reraise=True
        )
        try:
            r.call(print_msg)
        except Exception:
            logging.error('Test error 2')
    except Exception:
        logging.error('Received Exception')

在执行上述代码时。输出如下所示,无需重试

/Users/dmanna/PycharmProjects/demo/venv/bin/python /Users/dmanna/PycharmProjects/demo/retrier.py
25-11-2018:00:29:47,140 INFO     [retrier.py:21] Starting
25-11-2018:00:29:47,140 INFO     [retrier.py:8] Hello
25-11-2018:00:29:47,140 INFO     [retrier.py:9] World
25-11-2018:00:29:47,140 ERROR    [retrier.py:12] caught error
25-11-2018:00:29:47,141 ERROR    [retrier.py:31] Test error 2

Process finished with exit code 0

由于我在上面的代码中没有看到任何重试,有人可以告诉我出了什么问题吗?

【问题讨论】:

  • too many try constructions ) 检查r.call(print_msg()) 如果不是从try 结构调用它会如何工作。
  • 它是更大代码的一部分。我试图简化在此处发布的问题。但是,即使在 try 中调用,这也不应该有效吗?

标签: python python-2.7 python-tenacity


【解决方案1】:

已回答here。交叉发布答案

嗯,我认为您在这里没有正确使用 API:

r = tenacity.Retrying(
            tenacity.stop_after_attempt(2),
            tenacity.wait_incrementing(start=10, increment=100, max=1000),
            reraise=True
        )

这转化为:

r = tenacity.Retrying(
            sleep=tenacity.stop_after_attempt(2),
            stop=tenacity.wait_incrementing(start=10, increment=100, max=1000),
            reraise=True
        )

这不会做你想做的事。

你想要:

r = tenacity.Retrying(
            stop=tenacity.stop_after_attempt(2),
            wait=tenacity.wait_incrementing(start=10, increment=100, max=1000),
            reraise=True
        )

【讨论】:

    【解决方案2】:

    注意 Python 3.4+ 用户(支持注释),要在 Tenacity 中显式强制重试,一般用例是使用简单的 @retry 对其进行注释,然后引发特殊异常 TryAgain

    @retry
    def do_something():
        result = something_else()
        if result == 23:
           raise TryAgain
    

    这种方法类似于 Python 2.7 on this page 的答案也可以实现:

    import tenacity
    
    def do_something():
        result = something_else()
        if result == 23:
           raise TryAgain
    
    r = tenacity.Retrying(
            stop=tenacity.stop_after_attempt(2),
            wait=tenacity.wait_incrementing(start=10, increment=100, max=1000)
        )
    r.wraps(do_something)
    

    有关详细信息,请参阅API documentation on whether to retrysource that defines annotations

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-17
      • 1970-01-01
      • 1970-01-01
      • 2018-12-17
      • 2020-07-05
      相关资源
      最近更新 更多