tenacity icon indicating copy to clipboard operation
tenacity copied to clipboard

Getting error message from retry_state.outcome results in program termination.

Open wetbadger opened this issue 2 years ago • 4 comments

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.

wetbadger avatar Aug 10 '23 11:08 wetbadger

Hi Ben!

Did you try settings reraise=True?

@retry( reraise=True, wait=wait_random_exponential(min=1, max=60) ) async def do_some_stuff(): ...

Beso, Pablo.-

El jue, 10 ago 2023 a la(s) 08:19, Ben Alexander @.***) escribió:

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 https://tenacity.readthedocs.io/en/latest/, all I can see as an option to get the error message is the function retry() 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.retry()) #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? @.***:~/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.

— Reply to this email directly, view it on GitHub https://github.com/jd/tenacity/issues/413, or unsubscribe https://github.com/notifications/unsubscribe-auth/A2NTOTZW764RCYCS3QNB7I3XUS7VRANCNFSM6AAAAAA3LK73TM . You are receiving this because you are subscribed to this thread.Message ID: @.***>

-- [image: facebook] https://www.facebook.com/clicoh [image: linkedin] https://www.linkedin.com/company/clicoh [image: instagram] https://www.instagram.com/clicoh.ar Pablo Iaria

Software Engineer

Backend Core | clicOH @.*** https://clicoh.com/

piaria avatar Aug 10 '23 12:08 piaria

Hi Ben! Did you try settings reraise=True? @Retry( reraise=True, wait=wait_random_exponential(min=1, max=60) ) async def do_some_stuff(): ... Beso, Pablo.- El jue, 10 ago 2023 a la(s) 08:19, Ben Alexander @.***) escribió:

Hi Pablo,

Unfortunately, reraise=True didn't change the outcome (program termination). My function is not async. I suppose I could just run retry_state.outcome.results() in a thread that immediately terminates itself. That seems little janky.

Thanks, Ben

wetbadger avatar Aug 11 '23 01:08 wetbadger

Hi Ben,

I guess you'd have to use reraise=True and put your code in a try...except block so you can handle the exception properly.

Best, Pablo.-

El jue, 10 ago 2023 a la(s) 22:00, Ben Alexander @.***) escribió:

Hi Ben! Did you try settings reraise=True? @Retry https://github.com/Retry( reraise=True, wait=wait_random_exponential(min=1, max=60) ) async def do_some_stuff(): ... Beso, Pablo.- El jue, 10 ago 2023 a la(s) 08:19, Ben Alexander @.***) escribió:

Hi Pablo,

Unfortunately, reraise=True didn't change the outcome (program termination). My function is not async.

Thanks, Ben

— Reply to this email directly, view it on GitHub https://github.com/jd/tenacity/issues/413#issuecomment-1674101719, or unsubscribe https://github.com/notifications/unsubscribe-auth/A2NTOT2PMBWZ2477TXZY3JDXUV72NANCNFSM6AAAAAA3LK73TM . You are receiving this because you commented.Message ID: @.***>

-- [image: facebook] https://www.facebook.com/clicoh [image: linkedin] https://www.linkedin.com/company/clicoh [image: instagram] https://www.instagram.com/clicoh.ar Pablo Iaria

Software Engineer

Backend Core | clicOH @.*** https://clicoh.com/

piaria avatar Aug 11 '23 12:08 piaria

This solution works for me. Thanks!

wetbadger avatar Aug 11 '23 18:08 wetbadger