Skip to main content

Python retry utility

Project description

Travis Test Status

A simple way to auto retry a funciton that has the possibility of raising an error. You can either call retry directly or decorate a function to get it to retry. Intelligent matching logic allows you to retry certain exception while raising other exceptions.

Using retry as a function:

from retrypy import retry, check
def dummy_func():
    print "dummy_func called..."
    raise Exception("House")

retry.call(
    dummy_func,
    times=2
)

dummy_func called...
dummy_func called...
Exception: House

# usign a check method
retry.call(
    dummy_func,
    check = check.message_equals("foobar")
)
dummy_func called...
Exception: House

Params:

func: The function you want to call.
args: Positional args to be passed to func
kwargs: keywords args to be passed to func
exceptions: an array of Exception types you want to retry on.
check: A function that excepts Exception and Count and
    returns true if the function should be retried.
times: number of times to try the function (default=5)
wait: number of seconds to wait between tries (default=0) or a function
    that accepts try_count and returns a number of seconds to wait

Or you can use it as a decorator:

@retry.decorate(Exception, times=2)
def dummy_func():
    print "dummy_func called..."
    raise Exception("House")
dummy_func()

dummy_func called...
dummy_func called...
Exception: House

# custom wait function
@retry.decorate(Exception, times=2, wait=lambda n: 2*n)
def dummy_func():
    print "dummy_func called..."
    raise Exception("House")
dummy_func()

dummy_func called...
dummy_func called...
Exception: House

Params:

positional_args: All position arguments must be Exception types, these
    are the only types of exceptions we will retry.
check: see retrypy.call
times: see retrypy.call
wait: see retrypy.call

Or you can use it to wrap a function and return a new callable

def dummy_func():
    print "dummy_func called..."
    raise Exception("House")

func = retry.wrap(
    dummy_func,
    times=2
)
func()

dummy_func called...
dummy_func called...
Exception: House

Params: See Docs for retry.call

Installation

>> pip install retrypy

License:

See LICENSE

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

retrypy-0.0.2.tar.gz (3.2 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page