韧度输出重试信息?
Tenacity output the messages of retrying?
代码
import logging
from tenacity import retry, wait_incrementing, stop_after_attempt
import tenacity
@retry(wait=wait_incrementing(start=10, increment=10, max=100), stop=stop_after_attempt(3))
def print_msg():
logging.info('Hello')
logging.info("World")
raise Exception('Test error')
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')
print_msg()
输出
21-11-2018:12:40:48,586 INFO [retrier.py:18] Starting
21-11-2018:12:40:48,586 INFO [retrier.py:8] Hello
21-11-2018:12:40:48,586 INFO [retrier.py:9] World
21-11-2018:12:40:58,592 INFO [retrier.py:8] Hello
21-11-2018:12:40:58,592 INFO [retrier.py:9] World
21-11-2018:12:41:18,596 INFO [retrier.py:8] Hello
21-11-2018:12:41:18,596 INFO [retrier.py:9] World
21-11-2018:12:41:18,596 ERROR [retrier.py:22] Received Exception
....
如何记录它正在重试?如
Error. Retrying 1...
...
Error. Retrying 2...
...
您可以编写自己的回调函数以从 retry_state
获取 attempt_number
。
代码:
def log_attempt_number(retry_state):
"""return the result of the last call attempt"""
logging.error(f"Retrying: {retry_state.attempt_number}...")
记录尝试after
函数的每次调用,如下所示
@retry(wait=wait_incrementing(start=10, increment=10, max=100),
stop=stop_after_attempt(3),
after=log_attempt_number)
输出:
14-12-2021:19:01:26,716 INFO [<ipython-input-15-f6916dbe7ec1>:24] Starting
14-12-2021:19:01:26,718 INFO [<ipython-input-15-f6916dbe7ec1>:15] Hello
14-12-2021:19:01:26,720 INFO [<ipython-input-15-f6916dbe7ec1>:16] World
14-12-2021:19:01:26,723 ERROR [<ipython-input-15-f6916dbe7ec1>:11] Retrying: 1...
14-12-2021:19:01:36,731 INFO [<ipython-input-15-f6916dbe7ec1>:15] Hello
14-12-2021:19:01:36,733 INFO [<ipython-input-15-f6916dbe7ec1>:16] World
14-12-2021:19:01:36,735 ERROR [<ipython-input-15-f6916dbe7ec1>:11] Retrying: 2...
14-12-2021:19:01:56,756 INFO [<ipython-input-15-f6916dbe7ec1>:15] Hello
14-12-2021:19:01:56,758 INFO [<ipython-input-15-f6916dbe7ec1>:16] World
14-12-2021:19:01:56,759 ERROR [<ipython-input-15-f6916dbe7ec1>:11] Retrying: 3...
代码
import logging
from tenacity import retry, wait_incrementing, stop_after_attempt
import tenacity
@retry(wait=wait_incrementing(start=10, increment=10, max=100), stop=stop_after_attempt(3))
def print_msg():
logging.info('Hello')
logging.info("World")
raise Exception('Test error')
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')
print_msg()
输出
21-11-2018:12:40:48,586 INFO [retrier.py:18] Starting
21-11-2018:12:40:48,586 INFO [retrier.py:8] Hello
21-11-2018:12:40:48,586 INFO [retrier.py:9] World
21-11-2018:12:40:58,592 INFO [retrier.py:8] Hello
21-11-2018:12:40:58,592 INFO [retrier.py:9] World
21-11-2018:12:41:18,596 INFO [retrier.py:8] Hello
21-11-2018:12:41:18,596 INFO [retrier.py:9] World
21-11-2018:12:41:18,596 ERROR [retrier.py:22] Received Exception
....
如何记录它正在重试?如
Error. Retrying 1...
...
Error. Retrying 2...
...
您可以编写自己的回调函数以从 retry_state
获取 attempt_number
。
代码:
def log_attempt_number(retry_state):
"""return the result of the last call attempt"""
logging.error(f"Retrying: {retry_state.attempt_number}...")
记录尝试after
函数的每次调用,如下所示
@retry(wait=wait_incrementing(start=10, increment=10, max=100),
stop=stop_after_attempt(3),
after=log_attempt_number)
输出:
14-12-2021:19:01:26,716 INFO [<ipython-input-15-f6916dbe7ec1>:24] Starting
14-12-2021:19:01:26,718 INFO [<ipython-input-15-f6916dbe7ec1>:15] Hello
14-12-2021:19:01:26,720 INFO [<ipython-input-15-f6916dbe7ec1>:16] World
14-12-2021:19:01:26,723 ERROR [<ipython-input-15-f6916dbe7ec1>:11] Retrying: 1...
14-12-2021:19:01:36,731 INFO [<ipython-input-15-f6916dbe7ec1>:15] Hello
14-12-2021:19:01:36,733 INFO [<ipython-input-15-f6916dbe7ec1>:16] World
14-12-2021:19:01:36,735 ERROR [<ipython-input-15-f6916dbe7ec1>:11] Retrying: 2...
14-12-2021:19:01:56,756 INFO [<ipython-input-15-f6916dbe7ec1>:15] Hello
14-12-2021:19:01:56,758 INFO [<ipython-input-15-f6916dbe7ec1>:16] World
14-12-2021:19:01:56,759 ERROR [<ipython-input-15-f6916dbe7ec1>:11] Retrying: 3...