Skip to content

Instantly share code, notes, and snippets.

@kaidokert
Created April 16, 2016 22:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kaidokert/c68100c04c821361121540a8111df9a8 to your computer and use it in GitHub Desktop.
Save kaidokert/c68100c04c821361121540a8111df9a8 to your computer and use it in GitHub Desktop.
# 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)
@kaidokert
Copy link
Author

Quick test to try out backoff and retryz retry decorators. Python 2/3

There are multiple other libraries, tested the ones that have travis/coverage and no other dependencies.

https://libraries.io/pypi/retryz no deps, Travis/coverage
https://libraries.io/pypi/backoff no deps, Travis/coverage

Both are very similar. Can retry either on whitelisted exception or return value. Backoff has added timed backoff functions like exponential and fibonacci included, however it defaults to a 1-second sleep interval with jitter, thus making the simple case of immediate retry more verbose.

https://libraries.io/pypi/retryable no deps , Travis, however uses blacklist rather than whitelist for retryable exceptions
https://libraries.io/pypi/redo no deps, sparse documentation

https://libraries.io/pypi/retry direct deps: decorator, py
https://libraries.io/pypi/retryp direct deps: logtool, wrapt, six

More here : https://pypi.python.org/pypi?%3Aaction=search&term=retry+decorator&submit=search

@kaidokert
Copy link
Author

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