代码之家  ›  专栏  ›  技术社区  ›  tuk

Python在不使用decorator的情况下使用坚韧性重试

  •  0
  • tuk  · 技术社区  · 7 年前

    我正在尝试使用“坚韧”进行重试(没有装饰)。我的代码看起来就像解释的那样 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
    

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

    1 回复  |  直到 6 年前
        1
  •  1
  •   tuk    7 年前

    这个问题已经得到了回答 here . 交叉张贴答案

    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
  •  0
  •   ingyhere    6 年前

    注意:对于Python3.4+用户(支持注释),要明确地强制进行顽强的重试,一般的用例是使用简单的注释对其进行注释 @retry 然后提出特殊例外 TryAgain

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

    这种方法类似于Python2.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 retry 或者 source that defines annotations