auto-retry


Nameauto-retry JSON
Version 2023.12.8.1 PyPI version JSON
download
home_page
Summary
upload_time2023-12-08 09:45:03
maintainer
docs_urlNone
authorArter Tendean
requires_python>=3.8,<4.0
license
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # auto-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

```bash
pip install auto-retry
```

---

## API

### retry decorator

```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

```python
    from retry import retry
```

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

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

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

```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.'''
```

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

```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

```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.

```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": "",
    "name": "auto-retry",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8,<4.0",
    "maintainer_email": "",
    "keywords": "",
    "author": "Arter Tendean",
    "author_email": "arter.tendean.07@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/ff/81/966622c22842631fb99461bf5fc6f11c1a16be6602d075f7a5da8ee4d184/auto_retry-2023.12.8.1.tar.gz",
    "platform": null,
    "description": "# auto-retry\n\nEasy to use retry decorator.\n\n---\n\nFeatures\n\n- No external dependency (stdlib only).\n- (Optionally) Preserve function signatures (`pip install decorator`).\n- Original traceback, easy to debug.\n\n---\n\n## Installation\n\n```bash\npip install auto-retry\n```\n\n---\n\n## API\n\n### retry decorator\n\n```python\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```\n\nVarious retrying logic can be achieved by combination of arguments.\n\n#### Examples\n\n```python\n    from retry import retry\n```\n\n```python\n    @retry()\n    def make_trouble():\n        '''Retry until succeed'''\n```\n\n```python\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\n```python\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\n```python\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\n```python\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\n```python\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```\n\n### retry_call\n\n```python\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```\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```python\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\")\n```\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "",
    "version": "2023.12.8.1",
    "project_urls": null,
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "1d5446f62a72890b0b2085556db502a68b5d6f92c7e016cfbb90c9158c6fe883",
                "md5": "69f26d6a7afb85a8d4aa225010a4572e",
                "sha256": "f97452d023c12b7ba0ae5f57da71c090f309c18bbb68b452b083a4471a3aa831"
            },
            "downloads": -1,
            "filename": "auto_retry-2023.12.8.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "69f26d6a7afb85a8d4aa225010a4572e",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8,<4.0",
            "size": 4558,
            "upload_time": "2023-12-08T09:45:01",
            "upload_time_iso_8601": "2023-12-08T09:45:01.510655Z",
            "url": "https://files.pythonhosted.org/packages/1d/54/46f62a72890b0b2085556db502a68b5d6f92c7e016cfbb90c9158c6fe883/auto_retry-2023.12.8.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "ff81966622c22842631fb99461bf5fc6f11c1a16be6602d075f7a5da8ee4d184",
                "md5": "171c4c896d1cb074466457af3d6c7099",
                "sha256": "a78e91495d31e6c5665813d86ad044fc32ff3d17f07a8f777ec58b2848bd235c"
            },
            "downloads": -1,
            "filename": "auto_retry-2023.12.8.1.tar.gz",
            "has_sig": false,
            "md5_digest": "171c4c896d1cb074466457af3d6c7099",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8,<4.0",
            "size": 3310,
            "upload_time": "2023-12-08T09:45:03",
            "upload_time_iso_8601": "2023-12-08T09:45:03.148760Z",
            "url": "https://files.pythonhosted.org/packages/ff/81/966622c22842631fb99461bf5fc6f11c1a16be6602d075f7a5da8ee4d184/auto_retry-2023.12.8.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-12-08 09:45:03",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "auto-retry"
}
        
Elapsed time: 2.20826s