retrying
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
Not Understanding Your Question Please Define More!
Investigate the retry_on_result parameter, which allows you to specify another success criterion.
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