Created
April 16, 2016 22:44
-
-
Save kaidokert/c68100c04c821361121540a8111df9a8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# pip install retryz backoff | |
from __future__ import print_function | |
import random | |
import retryz | |
import backoff | |
def naughty_func(): | |
picky = random.randint(1, 3) | |
if picky == 1: | |
print('error') | |
raise ValueError("Not good") | |
elif picky == 2: | |
raise RuntimeError("All bad") | |
else: | |
print('okay') | |
@retryz.retry(on_error=ValueError) | |
def retryz_call(): | |
naughty_func() | |
@retryz.retry(on_error=ValueError, limit=2) | |
def retryz_limit(): | |
naughty_func() | |
@backoff.on_exception(backoff.constant, ValueError, | |
interval=0, jitter=lambda: 0) | |
def backoff_call(): | |
naughty_func() | |
@backoff.on_exception(backoff.constant, ValueError, | |
interval=0, jitter=lambda: 0, | |
max_tries=2) | |
def backoff_limit(): | |
naughty_func() | |
def test_decorated(function, reraise=True): | |
random.seed(1) | |
for _ in range(30): | |
print('--->{}'.format(function.__name__)) | |
try: | |
function() | |
except RuntimeError: | |
print('rt error') | |
except ValueError: | |
if reraise: # pragma: nocover | |
raise | |
else: | |
print('retry exceeded') | |
test_decorated(retryz_call) | |
test_decorated(retryz_limit, False) | |
test_decorated(backoff_call) | |
test_decorated(backoff_limit, False) |
Yep i saw aspectlib around too, but i specifically wanted to look at zero-dependency single purpose libs.
These are pretty different internally actually .. retryz uses threading to monitor timeouts, which might be somewhat unexpected to user
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Check out: http://python-aspectlib.readthedocs.org/en/latest/reference/aspectlib.contrib.html#aspectlib.contrib.retry