retry


Nameretry JSON
Version 0.9.2 PyPI version JSON
download
home_pagehttps://github.com/invl/retry
SummaryEasy to use retry decorator.
upload_time2016-05-11 13:58:51
maintainer
docs_urlNone
authorinvl
requires_python
licenseApache License 2.0
keywords
VCS
bugtrack_url
requirements decorator py
Travis-CI No Travis.
coveralls test coverage No coveralls.
            retry
=====

.. image:: https://pypip.in/d/retry/badge.png
        :target: https://pypi.python.org/pypi/retry/

.. image:: https://pypip.in/v/retry/badge.png
        :target: https://pypi.python.org/pypi/retry/

.. image:: https://pypip.in/license/retry/badge.png
        :target: https://pypi.python.org/pypi/retry/


Easy to use retry decorator.


Features
--------

- No external dependency (stdlib only).
- (Optionally) Preserve function signatures (`pip install decorator`).
- Original traceback, easy to debug.


Installation
------------

.. code-block:: bash

    $ pip install retry


API
---

retry decorator
^^^^^^^^^^^^^^^

.. code:: python

    def retry(exceptions=Exception, tries=-1, delay=0, max_delay=None, backoff=1, jitter=0, logger=logging_logger):
        """Return a retry decorator.

        :param exceptions: an exception or a tuple of exceptions to catch. default: Exception.
        :param tries: the maximum number of attempts. default: -1 (infinite).
        :param delay: initial delay between attempts. default: 0.
        :param max_delay: the maximum value of delay. default: None (no limit).
        :param backoff: multiplier applied to delay between attempts. default: 1 (no backoff).
        :param jitter: extra seconds added to delay between attempts. default: 0.
                       fixed if a number, random if a range tuple (min, max)
        :param logger: logger.warning(fmt, error, delay) will be called on failed attempts.
                       default: retry.logging_logger. if None, logging is disabled.
        """

Various retrying logic can be achieved by combination of arguments.


Examples
""""""""

.. code:: python

    from retry import retry

.. code:: python

    @retry()
    def make_trouble():
        '''Retry until succeed'''

.. code:: python

    @retry(ZeroDivisionError, tries=3, delay=2)
    def make_trouble():
        '''Retry on ZeroDivisionError, raise error after 3 attempts, sleep 2 seconds between attempts.'''

.. code:: python

    @retry((ValueError, TypeError), delay=1, backoff=2)
    def make_trouble():
        '''Retry on ValueError or TypeError, sleep 1, 2, 4, 8, ... seconds between attempts.'''

.. code:: python

    @retry((ValueError, TypeError), delay=1, backoff=2, max_delay=4)
    def make_trouble():
        '''Retry on ValueError or TypeError, sleep 1, 2, 4, 4, ... seconds between attempts.'''

.. code:: python

    @retry(ValueError, delay=1, jitter=1)
    def make_trouble():
        '''Retry on ValueError, sleep 1, 2, 3, 4, ... seconds between attempts.'''

.. code:: python

    # If you enable logging, you can get warnings like 'ValueError, retrying in
    # 1 seconds'
    if __name__ == '__main__':
        import logging
        logging.basicConfig()
        make_trouble()

retry_call
^^^^^^^^^^

.. code:: python

    def retry_call(f, fargs=None, fkwargs=None, exceptions=Exception, tries=-1, delay=0, max_delay=None, backoff=1,
                   jitter=0,
                   logger=logging_logger):
        """
        Calls a function and re-executes it if it failed.

        :param f: the function to execute.
        :param fargs: the positional arguments of the function to execute.
        :param fkwargs: the named arguments of the function to execute.
        :param exceptions: an exception or a tuple of exceptions to catch. default: Exception.
        :param tries: the maximum number of attempts. default: -1 (infinite).
        :param delay: initial delay between attempts. default: 0.
        :param max_delay: the maximum value of delay. default: None (no limit).
        :param backoff: multiplier applied to delay between attempts. default: 1 (no backoff).
        :param jitter: extra seconds added to delay between attempts. default: 0.
                       fixed if a number, random if a range tuple (min, max)
        :param logger: logger.warning(fmt, error, delay) will be called on failed attempts.
                       default: retry.logging_logger. if None, logging is disabled.
        :returns: the result of the f function.
        """

This is very similar to the decorator, except that it takes a function and its arguments as parameters. The use case behind it is to be able to dynamically adjust the retry arguments.

.. code:: python

    import requests

    from retry.api import retry_call


    def make_trouble(service, info=None):
        if not info:
            info = ''
        r = requests.get(service + info)
        return r.text


    def what_is_my_ip(approach=None):
        if approach == "optimistic":
            tries = 1
        elif approach == "conservative":
            tries = 3
        else:
            # skeptical
            tries = -1
        result = retry_call(make_trouble, fargs=["http://ipinfo.io/"], fkwargs={"info": "ip"}, tries=tries)
        print(result)

    what_is_my_ip("conservative")
            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/invl/retry",
    "name": "retry",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "",
    "author": "invl",
    "author_email": "invlpg@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/9d/72/75d0b85443fbc8d9f38d08d2b1b67cc184ce35280e4a3813cda2f445f3a4/retry-0.9.2.tar.gz",
    "platform": "UNKNOWN",
    "description": "retry\n=====\n\n.. image:: https://pypip.in/d/retry/badge.png\n        :target: https://pypi.python.org/pypi/retry/\n\n.. image:: https://pypip.in/v/retry/badge.png\n        :target: https://pypi.python.org/pypi/retry/\n\n.. image:: https://pypip.in/license/retry/badge.png\n        :target: https://pypi.python.org/pypi/retry/\n\n\nEasy to use retry decorator.\n\n\nFeatures\n--------\n\n- No external dependency (stdlib only).\n- (Optionally) Preserve function signatures (`pip install decorator`).\n- Original traceback, easy to debug.\n\n\nInstallation\n------------\n\n.. code-block:: bash\n\n    $ pip install retry\n\n\nAPI\n---\n\nretry decorator\n^^^^^^^^^^^^^^^\n\n.. code:: python\n\n    def retry(exceptions=Exception, tries=-1, delay=0, max_delay=None, backoff=1, jitter=0, logger=logging_logger):\n        \"\"\"Return a retry decorator.\n\n        :param exceptions: an exception or a tuple of exceptions to catch. default: Exception.\n        :param tries: the maximum number of attempts. default: -1 (infinite).\n        :param delay: initial delay between attempts. default: 0.\n        :param max_delay: the maximum value of delay. default: None (no limit).\n        :param backoff: multiplier applied to delay between attempts. default: 1 (no backoff).\n        :param jitter: extra seconds added to delay between attempts. default: 0.\n                       fixed if a number, random if a range tuple (min, max)\n        :param logger: logger.warning(fmt, error, delay) will be called on failed attempts.\n                       default: retry.logging_logger. if None, logging is disabled.\n        \"\"\"\n\nVarious retrying logic can be achieved by combination of arguments.\n\n\nExamples\n\"\"\"\"\"\"\"\"\n\n.. code:: python\n\n    from retry import retry\n\n.. code:: python\n\n    @retry()\n    def make_trouble():\n        '''Retry until succeed'''\n\n.. code:: python\n\n    @retry(ZeroDivisionError, tries=3, delay=2)\n    def make_trouble():\n        '''Retry on ZeroDivisionError, raise error after 3 attempts, sleep 2 seconds between attempts.'''\n\n.. code:: python\n\n    @retry((ValueError, TypeError), delay=1, backoff=2)\n    def make_trouble():\n        '''Retry on ValueError or TypeError, sleep 1, 2, 4, 8, ... seconds between attempts.'''\n\n.. code:: python\n\n    @retry((ValueError, TypeError), delay=1, backoff=2, max_delay=4)\n    def make_trouble():\n        '''Retry on ValueError or TypeError, sleep 1, 2, 4, 4, ... seconds between attempts.'''\n\n.. code:: python\n\n    @retry(ValueError, delay=1, jitter=1)\n    def make_trouble():\n        '''Retry on ValueError, sleep 1, 2, 3, 4, ... seconds between attempts.'''\n\n.. code:: python\n\n    # If you enable logging, you can get warnings like 'ValueError, retrying in\n    # 1 seconds'\n    if __name__ == '__main__':\n        import logging\n        logging.basicConfig()\n        make_trouble()\n\nretry_call\n^^^^^^^^^^\n\n.. code:: python\n\n    def retry_call(f, fargs=None, fkwargs=None, exceptions=Exception, tries=-1, delay=0, max_delay=None, backoff=1,\n                   jitter=0,\n                   logger=logging_logger):\n        \"\"\"\n        Calls a function and re-executes it if it failed.\n\n        :param f: the function to execute.\n        :param fargs: the positional arguments of the function to execute.\n        :param fkwargs: the named arguments of the function to execute.\n        :param exceptions: an exception or a tuple of exceptions to catch. default: Exception.\n        :param tries: the maximum number of attempts. default: -1 (infinite).\n        :param delay: initial delay between attempts. default: 0.\n        :param max_delay: the maximum value of delay. default: None (no limit).\n        :param backoff: multiplier applied to delay between attempts. default: 1 (no backoff).\n        :param jitter: extra seconds added to delay between attempts. default: 0.\n                       fixed if a number, random if a range tuple (min, max)\n        :param logger: logger.warning(fmt, error, delay) will be called on failed attempts.\n                       default: retry.logging_logger. if None, logging is disabled.\n        :returns: the result of the f function.\n        \"\"\"\n\nThis is very similar to the decorator, except that it takes a function and its arguments as parameters. The use case behind it is to be able to dynamically adjust the retry arguments.\n\n.. code:: python\n\n    import requests\n\n    from retry.api import retry_call\n\n\n    def make_trouble(service, info=None):\n        if not info:\n            info = ''\n        r = requests.get(service + info)\n        return r.text\n\n\n    def what_is_my_ip(approach=None):\n        if approach == \"optimistic\":\n            tries = 1\n        elif approach == \"conservative\":\n            tries = 3\n        else:\n            # skeptical\n            tries = -1\n        result = retry_call(make_trouble, fargs=[\"http://ipinfo.io/\"], fkwargs={\"info\": \"ip\"}, tries=tries)\n        print(result)\n\n    what_is_my_ip(\"conservative\")",
    "bugtrack_url": null,
    "license": "Apache License 2.0",
    "summary": "Easy to use retry decorator.",
    "version": "0.9.2",
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "md5": "68fc804e606821796333bc00c91abfc6",
                "sha256": "ccddf89761fa2c726ab29391837d4327f819ea14d244c232a1d24c67a2f98606"
            },
            "downloads": -1,
            "filename": "retry-0.9.2-py2.py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "68fc804e606821796333bc00c91abfc6",
            "packagetype": "bdist_wheel",
            "python_version": "py2.py3",
            "requires_python": null,
            "size": 7986,
            "upload_time": "2016-05-11T13:58:39",
            "upload_time_iso_8601": "2016-05-11T13:58:39.925822Z",
            "url": "https://files.pythonhosted.org/packages/4b/0d/53aea75710af4528a25ed6837d71d117602b01946b307a3912cb3cfcbcba/retry-0.9.2-py2.py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "md5": "81089364adc2d9a271f1b71eb9ef312b",
                "sha256": "f8bfa8b99b69c4506d6f5bd3b0aabf77f98cdb17f3c9fc3f5ca820033336fba4"
            },
            "downloads": -1,
            "filename": "retry-0.9.2.tar.gz",
            "has_sig": false,
            "md5_digest": "81089364adc2d9a271f1b71eb9ef312b",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 6448,
            "upload_time": "2016-05-11T13:58:51",
            "upload_time_iso_8601": "2016-05-11T13:58:51.541112Z",
            "url": "https://files.pythonhosted.org/packages/9d/72/75d0b85443fbc8d9f38d08d2b1b67cc184ce35280e4a3813cda2f445f3a4/retry-0.9.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2016-05-11 13:58:51",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "invl",
    "github_project": "retry",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "requirements": [
        {
            "name": "decorator",
            "specs": [
                [
                    ">=",
                    "3.4.2"
                ]
            ]
        },
        {
            "name": "py",
            "specs": [
                [
                    ">=",
                    "1.4.26"
                ],
                [
                    "<",
                    "2.0.0"
                ]
            ]
        }
    ],
    "tox": true,
    "lcname": "retry"
}
        
Elapsed time: 0.01448s