英文:
Getting error message from tenacity retry_state.outcome.result() results in program termination
问题
这段代码使用了Python的tenacity库来实现指数退避(exponential backoff)功能。
import tenacity
def log_attempt_number(retry_state):
print(f"Retrying: {retry_state.attempt_number}...")
print(retry_state.outcome)
@retry(wait=wait_random_exponential(min=1, max=60), stop=stop_after_attempt(100), after=log_attempt_number)
def throw_exception():
throw Exception("What is this exception?")
这段代码给出的输出如下:
Retrying: 1...
<Future at 0x7feeaf6354c0 state=finished raised Exception>
Retrying: 2...
<Future at 0x7feeaf401580 state=finished raised Exception>
Retrying: 3...
<Future at 0x7feeaf6354c0 state=finished raised Exception>
Retrying: 4...
<Future at 0x7feeaf401580 state=finished raised Exception>
Retrying: 5...
<Future at 0x7feeaf6354c0 state=finished raised Exception>
...
但是你想要看到错误消息而不是Future对象。根据tenacity库的文档,你可以使用retry_state.outcome.result()
来获取错误消息,而不会终止程序。
def log_attempt_number(retry_state):
print(f"Retrying: {retry_state.attempt_number}...")
print(retry_state.outcome.result()) # 这个函数不会终止程序
@retry(wait=wait_random_exponential(min=1, max=60), stop=stop_after_attempt(100), after=log_attempt_number)
def throw_exception():
throw Exception("What is this exception?")
这样修改后,你会得到错误消息而不会终止程序执行。
Retrying: 1...
What is this Exception?
worryword@WorryWord:~/Development/SOTests$
这回答了你的问题,你可以获取错误消息而不会终止程序。
英文:
I'm using the python tenacity library to do exponential backoff of a funtion.
import tenacity
def log_attempt_number(retry_state):
print(f"Retrying: {retry_state.attempt_number}...")
print(retry_state.outcome)
@retry(wait=wait_random_exponential(min=1, max=60), stop=stop_after_attempt(100), after=log_attempt_number)
def throw_exception():
throw Exception("What is this exception?")
This code gives me:
Retrying: 1...
<Future at 0x7feeaf6354c0 state=finished raised Exception>
Retrying: 2...
<Future at 0x7feeaf401580 state=finished raised Exception>
Retrying: 3...
<Future at 0x7feeaf6354c0 state=finished raised Exception>
Retrying: 4...
<Future at 0x7feeaf401580 state=finished raised Exception>
Retrying: 5...
<Future at 0x7feeaf6354c0 state=finished raised Exception>
...
But I want to see the error message, not the Future object.
On their website, all I can see as an option to get the error message is the function result() which gives me the error message and then terminates.
def log_attempt_number(retry_state):
print(f"Retrying: {retry_state.attempt_number}...")
print(retry_state.outcome.result()) #This function terminates the program
@retry(wait=wait_random_exponential(min=1, max=60), stop=stop_after_attempt(100),
after=log_attempt_number)
def throw_exception():
throw Exception("What is this exception?")
...
Retrying: 1...
What is this Exception?
worryword@WorryWord:~/Development/SOTests$
So my question is: how do I get the error message without terminating the program? I have an issue where the first error is not necessarily the 10th error, etc.
答案1
得分: 0
作为一种变通方法,我使用了类似于以下的try-except结构:
try:
retry_state.outcome.result()
except Exception as e:
print(e)
这样可以获取异常信息而不终止程序。
英文:
As a workaround, I used a try-except like so:
try:
retry_state.outcome.result()
except Exception as e:
print(e)
This gives you the exception without terminating the program.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论