retrying icon indicating copy to clipboard operation
retrying copied to clipboard

retrying

Open itaybz opened this issue 5 years ago • 3 comments

using the retrying package. How do I use it for retrying, and if the retries ends without a successful match, the code block leaves?

import random
from retrying import retry

@retry(wait_fixed=500, stop_max_delay=5000 )
def do_something_unreliable():
    result = random.randint(0, 10)
    print result
    if result != 11:
        raise IOError("Broken")
    else:
        return True

print do_something_unreliable()

I want the the raise IOError to be replaced with an option of returning, say, False

In other words, I want to use this decorator, in case of actual values is different than expected, while the actual is created frequently from other function

itaybz avatar Jul 30 '20 08:07 itaybz

Not Understanding Your Question Please Define More!

Harshil783 avatar Aug 03 '20 16:08 Harshil783

Investigate the retry_on_result parameter, which allows you to specify another success criterion.

labstersteve avatar Aug 13 '20 14:08 labstersteve

I want to call a function that always retries to compare one value to another and returns fails after this fails. Here is the code:

from retrying import retry

@retry( wait_fixed = 500, stop_max_delay = 40000 )
def _do_something_unreliable( actual, expected ):
	print actual, expected
	if actual != expected:  # retry does not succeed
		raise IOError( "Broken" )
	else:
		return True  # retry succeeds


def retry( actual, expect ):
	try:
		x = _do_something_unreliable( actual, expect )

	except:
		x = False

	print x
	return x


result = retry( actual=random.randint(0, 10), expect= 1 )

Any suggestions how to do that?

Obviously it does not work since the compassion is done at the "_do_something_unreliable" function

itaybz avatar Sep 02 '20 06:09 itaybz