Retrying
=========================
Retrying is an Apache 2.0 licensed general-purpose retrying library, written in
Python, to simplify the task of adding retry behavior to just about anything.
The simplest use case is retrying a flaky function whenever an Exception occurs
until a value is returned.
.. code-block:: python
import random
from retrying import retry
@retry
def do_something_unreliable():
if random.randint(0, 10) > 1:
raise IOError("Broken sauce, everything is hosed!!!111one")
else:
return "Awesome sauce!"
print do_something_unreliable()
Features
--------
- Generic Decorator API
- Specify stop condition (i.e. limit by number of attempts)
- Specify wait condition (i.e. exponential backoff sleeping between attempts)
- Customize retrying on Exceptions
- Customize retrying on expected returned result
Examples
----------
As you saw above, the default behavior is to retry forever without waiting.
.. code-block:: python
@retry
def never_give_up_never_surrender():
print "Retry forever ignoring Exceptions, don't wait between retries"
Let's be a little less persistent and set some boundaries, such as the number of attempts before giving up.
.. code-block:: python
@retry(stop_max_attempt_number=7)
def stop_after_7_attempts():
print "Stopping after 7 attempts"
We don't have all day, so let's set a boundary for how long we should be retrying stuff.
.. code-block:: python
@retry(stop_max_delay=10000)
def stop_after_10_s():
print "Stopping after 10 seconds"
Most things don't like to be polled as fast as possible, so let's just wait 2 seconds between retries.
.. code-block:: python
@retry(wait_fixed=2000)
def wait_2_s():
print "Wait 2 second between retries"
Some things perform best with a bit of randomness injected.
.. code-block:: python
@retry(wait_random_min=1000, wait_random_max=2000)
def wait_random_1_to_2_s():
print "Randomly wait 1 to 2 seconds between retries"
Then again, it's hard to beat exponential backoff when retrying distributed services and other remote endpoints.
.. code-block:: python
@retry(wait_exponential_multiplier=1000, wait_exponential_max=10000)
def wait_exponential_1000():
print "Wait 2^x * 1000 milliseconds between each retry, up to 10 seconds, then 10 seconds afterwards"
We have a few options for dealing with retries that raise specific or general exceptions, as in the cases here.
.. code-block:: python
def retry_if_io_error(exception):
"""Return True if we should retry (in this case when it's an IOError), False otherwise"""
return isinstance(exception, IOError)
@retry(retry_on_exception=retry_if_io_error)
def might_io_error():
print "Retry forever with no wait if an IOError occurs, raise any other errors"
@retry(retry_on_exception=retry_if_io_error, wrap_exception=True)
def only_raise_retry_error_when_not_io_error():
print "Retry forever with no wait if an IOError occurs, raise any other errors wrapped in RetryError"
We can also use the result of the function to alter the behavior of retrying.
.. code-block:: python
def retry_if_result_none(result):
"""Return True if we should retry (in this case when result is None), False otherwise"""
return result is None
@retry(retry_on_result=retry_if_result_none)
def might_return_none():
print "Retry forever ignoring Exceptions with no wait if return value is None"
Any combination of stop, wait, etc. is also supported to give you the freedom to mix and match.
Contribute
----------
#. Check for open issues or open a fresh issue to start a discussion around a feature idea or a bug.
#. Fork `the repository`_ on GitHub to start making your changes to the **master** branch (or branch off of it).
#. Write a test which shows that the bug was fixed or that the feature works as expected.
#. Send a pull request and bug the maintainer until it gets merged and published. :) Make sure to add yourself to AUTHORS_.
.. _`the repository`: http://github.com/groodt/retrying
.. _AUTHORS: https://github.com/groodt/retrying/blob/master/AUTHORS.rst
.. :changelog:
History
-------
1.3.4 (2022-09-03)
++++++++++++++++++
- Added Greg Roodt as maintainer
- Formatted code with black
- Updated repository references
1.3.3 (2014-12-14)
++++++++++++++++++
- Add minimum six version of 1.7.0 since anything less will break things
1.3.2 (2014-11-09)
++++++++++++++++++
- Ensure we wrap the decorated functions to prevent information loss
- Allow a jitter value to be passed in
1.3.1 (2014-09-30)
++++++++++++++++++
- Add requirements.txt to MANIFEST.in to fix pip installs
1.3.0 (2014-09-30)
++++++++++++++++++
- Add upstream six dependency, remove embedded six functionality
1.2.3 (2014-08-25)
++++++++++++++++++
- Add support for custom wait and stop functions
1.2.2 (2014-06-20)
++++++++++++++++++
- Bug fix to not raise a RetryError on failure when exceptions aren't being wrapped
1.2.1 (2014-05-05)
++++++++++++++++++
- Bug fix for explicitly passing in a wait type
1.2.0 (2014-05-04)
++++++++++++++++++
- Remove the need for explicit specification of stop/wait types when they can be inferred
- Add a little checking for exception propagation
1.1.0 (2014-03-31)
++++++++++++++++++
- Added proper exception propagation through reraising with Python 2.6, 2.7, and 3.2 compatibility
- Update test suite for behavior changes
1.0.1 (2013-03-20)
++++++++++++++++++
- Fixed a bug where classes not extending from the Python exception hierarchy could slip through
- Update test suite for custom Python exceptions
1.0.0 (2013-01-21)
++++++++++++++++++
- First stable, tested version now exists
- Apache 2.0 license applied
- Sanitizing some setup.py and test suite running
- Added Travis CI support
Raw data
{
"_id": null,
"home_page": "https://github.com/groodt/retrying",
"name": "retrying",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "decorator decorators retry retrying exception exponential backoff",
"author": "Greg Roodt",
"author_email": "",
"download_url": "https://files.pythonhosted.org/packages/ce/70/15ce8551d65b324e18c5aa6ef6998880f21ead51ebe5ed743c0950d7d9dd/retrying-1.3.4.tar.gz",
"platform": null,
"description": "Retrying\n=========================\nRetrying is an Apache 2.0 licensed general-purpose retrying library, written in\nPython, to simplify the task of adding retry behavior to just about anything.\n\n\nThe simplest use case is retrying a flaky function whenever an Exception occurs\nuntil a value is returned.\n\n.. code-block:: python\n\n import random\n from retrying import retry\n\n @retry\n def do_something_unreliable():\n if random.randint(0, 10) > 1:\n raise IOError(\"Broken sauce, everything is hosed!!!111one\")\n else:\n return \"Awesome sauce!\"\n\n print do_something_unreliable()\n\n\nFeatures\n--------\n\n- Generic Decorator API\n- Specify stop condition (i.e. limit by number of attempts)\n- Specify wait condition (i.e. exponential backoff sleeping between attempts)\n- Customize retrying on Exceptions\n- Customize retrying on expected returned result\n\n\nExamples\n----------\n\nAs you saw above, the default behavior is to retry forever without waiting.\n\n.. code-block:: python\n\n @retry\n def never_give_up_never_surrender():\n print \"Retry forever ignoring Exceptions, don't wait between retries\"\n\n\nLet's be a little less persistent and set some boundaries, such as the number of attempts before giving up.\n\n.. code-block:: python\n\n @retry(stop_max_attempt_number=7)\n def stop_after_7_attempts():\n print \"Stopping after 7 attempts\"\n\nWe don't have all day, so let's set a boundary for how long we should be retrying stuff.\n\n.. code-block:: python\n\n @retry(stop_max_delay=10000)\n def stop_after_10_s():\n print \"Stopping after 10 seconds\"\n\nMost things don't like to be polled as fast as possible, so let's just wait 2 seconds between retries.\n\n.. code-block:: python\n\n @retry(wait_fixed=2000)\n def wait_2_s():\n print \"Wait 2 second between retries\"\n\n\nSome things perform best with a bit of randomness injected.\n\n.. code-block:: python\n\n @retry(wait_random_min=1000, wait_random_max=2000)\n def wait_random_1_to_2_s():\n print \"Randomly wait 1 to 2 seconds between retries\"\n\nThen again, it's hard to beat exponential backoff when retrying distributed services and other remote endpoints.\n\n.. code-block:: python\n\n @retry(wait_exponential_multiplier=1000, wait_exponential_max=10000)\n def wait_exponential_1000():\n print \"Wait 2^x * 1000 milliseconds between each retry, up to 10 seconds, then 10 seconds afterwards\"\n\n\nWe have a few options for dealing with retries that raise specific or general exceptions, as in the cases here.\n\n.. code-block:: python\n\n def retry_if_io_error(exception):\n \"\"\"Return True if we should retry (in this case when it's an IOError), False otherwise\"\"\"\n return isinstance(exception, IOError)\n\n @retry(retry_on_exception=retry_if_io_error)\n def might_io_error():\n print \"Retry forever with no wait if an IOError occurs, raise any other errors\"\n\n @retry(retry_on_exception=retry_if_io_error, wrap_exception=True)\n def only_raise_retry_error_when_not_io_error():\n print \"Retry forever with no wait if an IOError occurs, raise any other errors wrapped in RetryError\"\n\nWe can also use the result of the function to alter the behavior of retrying.\n\n.. code-block:: python\n\n def retry_if_result_none(result):\n \"\"\"Return True if we should retry (in this case when result is None), False otherwise\"\"\"\n return result is None\n\n @retry(retry_on_result=retry_if_result_none)\n def might_return_none():\n print \"Retry forever ignoring Exceptions with no wait if return value is None\"\n\n\nAny combination of stop, wait, etc. is also supported to give you the freedom to mix and match.\n\nContribute\n----------\n\n#. Check for open issues or open a fresh issue to start a discussion around a feature idea or a bug.\n#. Fork `the repository`_ on GitHub to start making your changes to the **master** branch (or branch off of it).\n#. Write a test which shows that the bug was fixed or that the feature works as expected.\n#. Send a pull request and bug the maintainer until it gets merged and published. :) Make sure to add yourself to AUTHORS_.\n\n.. _`the repository`: http://github.com/groodt/retrying\n.. _AUTHORS: https://github.com/groodt/retrying/blob/master/AUTHORS.rst\n\n\n.. :changelog:\n\nHistory\n-------\n1.3.4 (2022-09-03)\n++++++++++++++++++\n- Added Greg Roodt as maintainer\n- Formatted code with black\n- Updated repository references\n\n1.3.3 (2014-12-14)\n++++++++++++++++++\n- Add minimum six version of 1.7.0 since anything less will break things\n\n1.3.2 (2014-11-09)\n++++++++++++++++++\n- Ensure we wrap the decorated functions to prevent information loss\n- Allow a jitter value to be passed in\n\n1.3.1 (2014-09-30)\n++++++++++++++++++\n- Add requirements.txt to MANIFEST.in to fix pip installs\n\n1.3.0 (2014-09-30)\n++++++++++++++++++\n- Add upstream six dependency, remove embedded six functionality\n\n1.2.3 (2014-08-25)\n++++++++++++++++++\n- Add support for custom wait and stop functions\n\n1.2.2 (2014-06-20)\n++++++++++++++++++\n- Bug fix to not raise a RetryError on failure when exceptions aren't being wrapped\n\n1.2.1 (2014-05-05)\n++++++++++++++++++\n- Bug fix for explicitly passing in a wait type\n\n1.2.0 (2014-05-04)\n++++++++++++++++++\n- Remove the need for explicit specification of stop/wait types when they can be inferred\n- Add a little checking for exception propagation\n\n1.1.0 (2014-03-31)\n++++++++++++++++++\n- Added proper exception propagation through reraising with Python 2.6, 2.7, and 3.2 compatibility\n- Update test suite for behavior changes\n\n1.0.1 (2013-03-20)\n++++++++++++++++++\n- Fixed a bug where classes not extending from the Python exception hierarchy could slip through\n- Update test suite for custom Python exceptions\n\n1.0.0 (2013-01-21)\n++++++++++++++++++\n- First stable, tested version now exists\n- Apache 2.0 license applied\n- Sanitizing some setup.py and test suite running\n- Added Travis CI support\n",
"bugtrack_url": null,
"license": "Apache 2.0",
"summary": "Retrying",
"version": "1.3.4",
"split_keywords": [
"decorator",
"decorators",
"retry",
"retrying",
"exception",
"exponential",
"backoff"
],
"urls": [
{
"comment_text": "",
"digests": {
"md5": "5e9234f0da2655cd043afbc6f9679dc5",
"sha256": "8cc4d43cb8e1125e0ff3344e9de678fefd85db3b750b81b2240dc0183af37b35"
},
"downloads": -1,
"filename": "retrying-1.3.4-py3-none-any.whl",
"has_sig": false,
"md5_digest": "5e9234f0da2655cd043afbc6f9679dc5",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 11602,
"upload_time": "2022-11-25T09:57:47",
"upload_time_iso_8601": "2022-11-25T09:57:47.494182Z",
"url": "https://files.pythonhosted.org/packages/8f/04/9e36f28be4c0532c0e9207ff9dc01fb13a2b0eb036476a213b0000837d0e/retrying-1.3.4-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"md5": "60ec4690e80e903117b91bc74a220116",
"sha256": "345da8c5765bd982b1d1915deb9102fd3d1f7ad16bd84a9700b85f64d24e8f3e"
},
"downloads": -1,
"filename": "retrying-1.3.4.tar.gz",
"has_sig": false,
"md5_digest": "60ec4690e80e903117b91bc74a220116",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 10929,
"upload_time": "2022-11-25T09:57:49",
"upload_time_iso_8601": "2022-11-25T09:57:49.430619Z",
"url": "https://files.pythonhosted.org/packages/ce/70/15ce8551d65b324e18c5aa6ef6998880f21ead51ebe5ed743c0950d7d9dd/retrying-1.3.4.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2022-11-25 09:57:49",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "groodt",
"github_project": "retrying",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"requirements": [
{
"name": "six",
"specs": [
[
">=",
"1.7.0"
]
]
}
],
"lcname": "retrying"
}