从 tenacity retry_state.outcome.result() 获取错误消息会导致程序终止。

huangapple go评论97阅读模式
英文:

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&quot;Retrying: {retry_state.attempt_number}...&quot;)
    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(&quot;What is this exception?&quot;)

This code gives me:

Retrying: 1...
&lt;Future at 0x7feeaf6354c0 state=finished raised Exception&gt;
Retrying: 2...
&lt;Future at 0x7feeaf401580 state=finished raised Exception&gt;
Retrying: 3...
&lt;Future at 0x7feeaf6354c0 state=finished raised Exception&gt;
Retrying: 4...
&lt;Future at 0x7feeaf401580 state=finished raised Exception&gt;
Retrying: 5...
&lt;Future at 0x7feeaf6354c0 state=finished raised Exception&gt;

...

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&quot;Retrying: {retry_state.attempt_number}...&quot;)
    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(&quot;What is this exception?&quot;)

...

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.

huangapple
  • 本文由 发表于 2023年8月10日 19:25:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76875289.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定