Redo – Utilities to retry Python callables

We deal with a lot of flaky things in RelEng. The network can drop. Code can have race conditions. Servers can go offline temporarily. Freak errors can happen (more often than you’d think). One of the ways we’ve learned to cope with this is to add “retry” behaviour to damn near everything that could fail intermittently. We use it so much that we’ve got a Python library and command line tool that are used all over the place.

Last week I finally got around to packaging and publishing ours, and I’m happy to present: Redo – Utilities to retry Python callables. Redo provides a decorator, context manager, plain old function, and even a command line tool to retry all sorts of things that may break. It’s very simple to use, here’s some examples from the docs:
The plain old function:

def maybe_raises(foo, bar=1):
    ...
    return 1

def cleanup():
    os.rmtree("/tmp/dirtydir")

ret = retry(maybe_raises, retry_exceptions=(HTTPError,),
            cleanup=cleanup, args=1, kwargs={"bar": 2})

The decorator:

from redo import retriable

@retriable()
def foo()
    ...

@retriable(attempts=100, sleeptime=10)
def bar():
    ...

The context manager:

def foo(a, b):
    ...

with retrying(foo, retry_exceptions=(HTTPError,)) as retrying_foo:
    r = retrying_foo(1, 3)

You can grab version 1.0 from PyPI, or find it on Github, where you can send issues or pull requests.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>