[英]Python retry using tenacity without decorator
I am trying to do a retry using tenacity (without decorator).我正在尝试使用坚韧(没有装饰器)进行重试。 My code looks like as explained 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')
On executing the above code.在执行上面的代码。 The output looks like below with no retry
输出如下所示,无需重试
/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
Can someone let me know what is going wrong as I am not seeing any retry in the above code?有人可以让我知道出了什么问题,因为我在上面的代码中没有看到任何重试吗?
This has been answered here .这已在此处得到解答。 Cross posting the answer
交叉发布答案
Hum I don't think you're using the API correctly here:
嗯,我认为您在这里没有正确使用 API:
r = tenacity.Retrying( tenacity.stop_after_attempt(2), tenacity.wait_incrementing(start=10, increment=100, max=1000), reraise=True )
This translates to:
这转化为:
r = tenacity.Retrying( sleep=tenacity.stop_after_attempt(2), stop=tenacity.wait_incrementing(start=10, increment=100, max=1000), reraise=True )
Which is not going to do what you want.
这不会做你想做的。
You want:
你要:
r = tenacity.Retrying( stop=tenacity.stop_after_attempt(2), wait=tenacity.wait_incrementing(start=10, increment=100, max=1000), reraise=True )
Note for Python 3.4+ users (which supports annotations), to explicitly force a retry in Tenacity the general use case is to annotate it with a simple @retry
and then raise the special Exception TryAgain
.请注意 Python 3.4+ 用户(支持注释),要在 Tenacity 中显式强制重试,一般用例是使用简单的
@retry
对其进行注释,然后引发特殊的 Exception TryAgain
。
@retry
def do_something():
result = something_else()
if result == 23:
raise TryAgain
This approach similar to the answer for Python 2.7 on this page can also be implemented:也可以实现类似于此页面上Python 2.7 答案的这种方法:
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)
For further detail, see the API documentation on whether to retry or thesource that defines annotations .有关更多详细信息,请参阅有关是否重试或定义注释的源的API 文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.