Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do something / nothing when max retries run out. #61

Open
ubehera opened this issue Aug 24, 2016 · 3 comments
Open

Do something / nothing when max retries run out. #61

ubehera opened this issue Aug 24, 2016 · 3 comments

Comments

@ubehera
Copy link

ubehera commented Aug 24, 2016

I'm doing a broad except to catch everything and then not raising it. To make retrying work, I had to catch and raise the IOError specifically. But if the max retries run out, I don't wan't it to raise and fail. Is there a way to do this?

@PegasusWang
Copy link

PegasusWang commented Sep 6, 2016

Can we add a default return value when reach max retries ?
add a default_return_value to Retrying constructor, if default_return_value is not None and reach max retry times just return this default value and do nothing.

or

def f():
    @retry(retry_on_result=retry_if_result_none, stop_max_attempt_number=7,
        after_attempts=after_attempts, wrap_exception=True)
    def might_return_none():
        print "Retry forever ignoring Exceptions with no wait if return value is None"
        return None
        # return 1
    try:
        might_return_none()
    except RetryError as e:
        print(e)

then just call f() instead of might_reutrn_none()

@gulducat
Copy link

Hey there @ubehera and @PegasusWang -- I had need of a similar thing and ended up writing #78

If you're still around and have thoughts, it'd be pretty nifty if you wanted to chime in on that PR.

@gulducat
Copy link

gulducat commented May 2, 2018

Just in case someone stumbles across this (like I did), I made a change to tenacity (after seeing #65) that covers this.

from tenacity import retry, stop_after_attempt

Do nothing (return None):

def do_nothing(eh):
    return

@retry(stop=stop_after_attempt(3), retry_error_callback=do_nothing)
def is_ok_to_fail():
    raise Exception('aw dang')

or, to return a default value (using lambda here because why not)

@retry(stop=stop_after_attempt(3), retry_error_callback=lambda _: True)
def return_true_on_exception():
    raise Exception('aw shucks')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants