Skip to main content

Retry decorator with a bunch of configuration parameters.

Project description

Build Status Coverage Status

Introduction

Function decorator that helps to retry the function under certain criteria.

This package contains the retry decorator and a bunch of configuration parameters for this decorator.

Tested on python 2.7 and python 3.4.

For quick start, check the tutorial section of this page. Check test_retryz.py for detail examples.

Installation

pip install retryz

Tutorial

  • Retry if ValueError is caught.

@retry(on_error=ValueError)
def my_func():
    ...
  • Retry if ValueError or TypeError is caught.

@retry(on_errors=[ValueError, TypeError])
def my_func():
    ...
  • Retry until TypeError is caught.

@retry(unless_error=TypeError)
def my_func():
    ...
  • Retry until TypeError or AttributeError is caught.

@retry(unless_errors=[TypeError, AttributeError])
def my_func():
    ...
  • Retry until error_call_back returns False. Note that callback could be a function or an instancemethod. But it could not be a staticmethod or class method. It takes one parameter which is the error instance raised by the decorated function.

def _error_call_back(self, ex):
    assert_that(ex, instance_of(TypeError))
    return self.call_count != 4

@retry(error_call_back=_error_call_back)
def error_call_back(self):
    ...
  • Retry if returns certain value.

@retry(on_return=True)
def my_func(self):
    ...
  • Retry if return value in the list.

@retry(on_returns=[1, 2, 3, 4, 5])
def my_func(self):
    ...
  • Retry until certain value is returned.

@retry(unless_return=4)
def my_func(self):
    ...
  • Retry until any of the value is returned.

@retry(unless_returns=[3, 4])
def my_func(self):
    ...
  • Retry until return_call_back returns False. Note that callback could be a function or an instancemethod. But it could not be a staticmethod or class method. It takes one parameter which is the return value of the decorated function.

def _return_call_back(ret):
    return 4 + ret < 7

@retry(return_call_back=_return_call_back)
def my_func(self):
    ...
  • Retry until timeout (in seconds)

@retry(timeout=0.1)
def my_func():
    ...
  • Retry maximum X times.

@retry(limit=3)
def my_func():
    ...
  • Wait X seconds between each retry.

@retry(wait=0.1, timeout=0.3)
def my_func():
    ...
  • Use wait_callback to return the amount of seconds to wait. The wait_callback could be a function or a instance method. It takes one parameter which is the current count of retry.

def _wait_callback(self, tried):
    return 2 ** tried

@retry(wait=_wait_callback, timeout=0.1)
def my_func():
    ...

To file issue, please visit:

https://github.com/jealous/retryz

Contact author:

jealous@163.com

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

retryz-0.1.0.zip (9.6 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