gretry


Namegretry JSON
Version 2.0.6 PyPI version JSON
download
home_pagehttps://github.com/Leviathangk/gretry
Summary一个错误重试装饰器
upload_time2023-09-15 13:05:47
maintainer
docs_urlNone
author郭一会儿
requires_python
licenseMIT Licence
keywords retry retrying
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # gretry

包含以下两个模块

- 错误重试装饰器 retry
- 错误跳转装饰器 error_jump

# 安装

```
pip install gretry
```

# 模块介绍

## retry 模块

用来进行错误重试  
参数如下:

- retry: 失败重试次数(默认 3 次)
- delay: 错误重试间隔(默认 0s)
- on_exceptions: 哪些报错才重试(默认 都重试)
- ignore_exceptions: 哪些报错不重试(默认 无)
- callback: 执行成功回调结果函数
- error_callback: 执行失败回调结果函数
- raise_exception: 一直失败,最后是否需要抛出错误(默认 True)

注意:多个 @retry 装饰器套着用是不可取的,因为一个装饰器重新执行程序,那么其他装饰器也会再次执行

## error_jump 模块

用于执行错误时跳转,当然,正确也可以跳转  
参数如下:

- on_exceptions: 哪些报错才跳转(默认 都跳转)
- ignore_exceptions: 哪些报错不执行跳转(默认 无)
- callback: 执行成功回调结果函数
- error_callback: 执行失败回调结果函数
- raise_exception: 是否需要抛出错误(默认 True)

注意:有多个 @error_jump 装饰器时,不要使用 raise_exception = False 不然其余装饰器无法捕获错误

# 注意点

# exception

on_exceptions 和 ignore_exceptions 只有一个能被设置

## 当有 callback 参数时

- 当有正确的结果时,会将结果作为参数放入 callback 并执行
- 有 callback 则不会返回结果
- 无正确结果则直接抛出

## 当有 error_callback 参数时

- 当执行失败时,会在抛出前,将函数的执行参数,作为参数放入 error_callback 并执行

## 错误的继承类的问题

比如 FileExistsError 类型是 OSError 的子类,这里判断的时候是 FileExistsError 就是 FileExistsError,不会向上识别

# 示例-retry

## 示例 1

【demo】

```
from gretry import retry


def failed(name):
    print('failed', name)


def success(result):
    print('success', result)


@retry(max_retry=3, delay=1, callback=success, error_callback=failed)
def run(name):
    raise FileExistsError('文件不存在!')


if __name__ == '__main__':
    run(name='郭一会儿')

```

【输出】

```
failed 郭一会儿
Traceback (most recent call last):
  File "D:\Program\Python\Pypi\gretry\test.py", line 18, in <module>
    run(name='郭一会儿')
  File "D:\Program\Python\Pypi\gretry\gretry\gretry.py", line 136, in wrapper
    return self.runner(func, *args, **kwargs)
  File "D:\Program\Python\Pypi\gretry\gretry\gretry.py", line 103, in runner
    raise exception
  File "D:\Program\Python\Pypi\gretry\gretry\gretry.py", line 74, in runner
    result = func(*args, **kwargs)
  File "D:\Program\Python\Pypi\gretry\test.py", line 14, in run
    raise FileExistsError('文件不存在!')
FileExistsError: 文件不存在!
```

## 示例 2

on_exceptions、ignore_exceptions 参数可以是错误的类型或者是列表

```
@retry(max_retry=3, on_exceptions=FileExistsError, ignore_exceptions=[OSError])
def run(name):
    raise FileExistsError('文件不存在!')
```

# 示例-error_jump

```
from gretry import retry, error_jump


def error(s):
    print('error1')


def error2(s):
    print('error2')


def success(res):
    print('success', res)


@error_jump(on_exceptions=FileExistsError, error_callback=error)
@error_jump(on_exceptions=FileNotFoundError, error_callback=error2)
@error_jump(callback=success)
def run(s):
    raise FileExistsError('1')


if __name__ == '__main__':
    run(2)
```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/Leviathangk/gretry",
    "name": "gretry",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "retry,retrying",
    "author": "\u90ed\u4e00\u4f1a\u513f",
    "author_email": "1015295213@qq.com",
    "download_url": "https://files.pythonhosted.org/packages/8d/4a/888f76719b87c2843a32f621e6be5105d7c24842a3597b9acb4d18c0ca80/gretry-2.0.6.tar.gz",
    "platform": "any",
    "description": "# gretry\r\n\r\n\u5305\u542b\u4ee5\u4e0b\u4e24\u4e2a\u6a21\u5757\r\n\r\n- \u9519\u8bef\u91cd\u8bd5\u88c5\u9970\u5668 retry\r\n- \u9519\u8bef\u8df3\u8f6c\u88c5\u9970\u5668 error_jump\r\n\r\n# \u5b89\u88c5\r\n\r\n```\r\npip install gretry\r\n```\r\n\r\n# \u6a21\u5757\u4ecb\u7ecd\r\n\r\n## retry \u6a21\u5757\r\n\r\n\u7528\u6765\u8fdb\u884c\u9519\u8bef\u91cd\u8bd5  \r\n\u53c2\u6570\u5982\u4e0b\uff1a\r\n\r\n- retry: \u5931\u8d25\u91cd\u8bd5\u6b21\u6570\uff08\u9ed8\u8ba4 3 \u6b21\uff09\r\n- delay: \u9519\u8bef\u91cd\u8bd5\u95f4\u9694\uff08\u9ed8\u8ba4 0s\uff09\r\n- on_exceptions: \u54ea\u4e9b\u62a5\u9519\u624d\u91cd\u8bd5\uff08\u9ed8\u8ba4 \u90fd\u91cd\u8bd5\uff09\r\n- ignore_exceptions: \u54ea\u4e9b\u62a5\u9519\u4e0d\u91cd\u8bd5\uff08\u9ed8\u8ba4 \u65e0\uff09\r\n- callback: \u6267\u884c\u6210\u529f\u56de\u8c03\u7ed3\u679c\u51fd\u6570\r\n- error_callback: \u6267\u884c\u5931\u8d25\u56de\u8c03\u7ed3\u679c\u51fd\u6570\r\n- raise_exception: \u4e00\u76f4\u5931\u8d25\uff0c\u6700\u540e\u662f\u5426\u9700\u8981\u629b\u51fa\u9519\u8bef\uff08\u9ed8\u8ba4 True\uff09\r\n\r\n\u6ce8\u610f\uff1a\u591a\u4e2a @retry \u88c5\u9970\u5668\u5957\u7740\u7528\u662f\u4e0d\u53ef\u53d6\u7684\uff0c\u56e0\u4e3a\u4e00\u4e2a\u88c5\u9970\u5668\u91cd\u65b0\u6267\u884c\u7a0b\u5e8f\uff0c\u90a3\u4e48\u5176\u4ed6\u88c5\u9970\u5668\u4e5f\u4f1a\u518d\u6b21\u6267\u884c\r\n\r\n## error_jump \u6a21\u5757\r\n\r\n\u7528\u4e8e\u6267\u884c\u9519\u8bef\u65f6\u8df3\u8f6c\uff0c\u5f53\u7136\uff0c\u6b63\u786e\u4e5f\u53ef\u4ee5\u8df3\u8f6c  \r\n\u53c2\u6570\u5982\u4e0b\uff1a\r\n\r\n- on_exceptions: \u54ea\u4e9b\u62a5\u9519\u624d\u8df3\u8f6c\uff08\u9ed8\u8ba4 \u90fd\u8df3\u8f6c\uff09\r\n- ignore_exceptions: \u54ea\u4e9b\u62a5\u9519\u4e0d\u6267\u884c\u8df3\u8f6c\uff08\u9ed8\u8ba4 \u65e0\uff09\r\n- callback: \u6267\u884c\u6210\u529f\u56de\u8c03\u7ed3\u679c\u51fd\u6570\r\n- error_callback: \u6267\u884c\u5931\u8d25\u56de\u8c03\u7ed3\u679c\u51fd\u6570\r\n- raise_exception: \u662f\u5426\u9700\u8981\u629b\u51fa\u9519\u8bef\uff08\u9ed8\u8ba4 True\uff09\r\n\r\n\u6ce8\u610f\uff1a\u6709\u591a\u4e2a @error_jump \u88c5\u9970\u5668\u65f6\uff0c\u4e0d\u8981\u4f7f\u7528 raise_exception = False \u4e0d\u7136\u5176\u4f59\u88c5\u9970\u5668\u65e0\u6cd5\u6355\u83b7\u9519\u8bef\r\n\r\n# \u6ce8\u610f\u70b9\r\n\r\n# exception\r\n\r\non_exceptions \u548c ignore_exceptions \u53ea\u6709\u4e00\u4e2a\u80fd\u88ab\u8bbe\u7f6e\r\n\r\n## \u5f53\u6709 callback \u53c2\u6570\u65f6\r\n\r\n- \u5f53\u6709\u6b63\u786e\u7684\u7ed3\u679c\u65f6\uff0c\u4f1a\u5c06\u7ed3\u679c\u4f5c\u4e3a\u53c2\u6570\u653e\u5165 callback \u5e76\u6267\u884c\r\n- \u6709 callback \u5219\u4e0d\u4f1a\u8fd4\u56de\u7ed3\u679c\r\n- \u65e0\u6b63\u786e\u7ed3\u679c\u5219\u76f4\u63a5\u629b\u51fa\r\n\r\n## \u5f53\u6709 error_callback \u53c2\u6570\u65f6\r\n\r\n- \u5f53\u6267\u884c\u5931\u8d25\u65f6\uff0c\u4f1a\u5728\u629b\u51fa\u524d\uff0c\u5c06\u51fd\u6570\u7684\u6267\u884c\u53c2\u6570\uff0c\u4f5c\u4e3a\u53c2\u6570\u653e\u5165 error_callback \u5e76\u6267\u884c\r\n\r\n## \u9519\u8bef\u7684\u7ee7\u627f\u7c7b\u7684\u95ee\u9898\r\n\r\n\u6bd4\u5982 FileExistsError \u7c7b\u578b\u662f OSError \u7684\u5b50\u7c7b\uff0c\u8fd9\u91cc\u5224\u65ad\u7684\u65f6\u5019\u662f FileExistsError \u5c31\u662f FileExistsError\uff0c\u4e0d\u4f1a\u5411\u4e0a\u8bc6\u522b\r\n\r\n# \u793a\u4f8b-retry\r\n\r\n## \u793a\u4f8b 1\r\n\r\n\u3010demo\u3011\r\n\r\n```\r\nfrom gretry import retry\r\n\r\n\r\ndef failed(name):\r\n    print('failed', name)\r\n\r\n\r\ndef success(result):\r\n    print('success', result)\r\n\r\n\r\n@retry(max_retry=3, delay=1, callback=success, error_callback=failed)\r\ndef run(name):\r\n    raise FileExistsError('\u6587\u4ef6\u4e0d\u5b58\u5728\uff01')\r\n\r\n\r\nif __name__ == '__main__':\r\n    run(name='\u90ed\u4e00\u4f1a\u513f')\r\n\r\n```\r\n\r\n\u3010\u8f93\u51fa\u3011\r\n\r\n```\r\nfailed \u90ed\u4e00\u4f1a\u513f\r\nTraceback (most recent call last):\r\n  File \"D:\\Program\\Python\\Pypi\\gretry\\test.py\", line 18, in <module>\r\n    run(name='\u90ed\u4e00\u4f1a\u513f')\r\n  File \"D:\\Program\\Python\\Pypi\\gretry\\gretry\\gretry.py\", line 136, in wrapper\r\n    return self.runner(func, *args, **kwargs)\r\n  File \"D:\\Program\\Python\\Pypi\\gretry\\gretry\\gretry.py\", line 103, in runner\r\n    raise exception\r\n  File \"D:\\Program\\Python\\Pypi\\gretry\\gretry\\gretry.py\", line 74, in runner\r\n    result = func(*args, **kwargs)\r\n  File \"D:\\Program\\Python\\Pypi\\gretry\\test.py\", line 14, in run\r\n    raise FileExistsError('\u6587\u4ef6\u4e0d\u5b58\u5728\uff01')\r\nFileExistsError: \u6587\u4ef6\u4e0d\u5b58\u5728\uff01\r\n```\r\n\r\n## \u793a\u4f8b 2\r\n\r\non_exceptions\u3001ignore_exceptions \u53c2\u6570\u53ef\u4ee5\u662f\u9519\u8bef\u7684\u7c7b\u578b\u6216\u8005\u662f\u5217\u8868\r\n\r\n```\r\n@retry(max_retry=3, on_exceptions=FileExistsError, ignore_exceptions=[OSError])\r\ndef run(name):\r\n    raise FileExistsError('\u6587\u4ef6\u4e0d\u5b58\u5728\uff01')\r\n```\r\n\r\n# \u793a\u4f8b-error_jump\r\n\r\n```\r\nfrom gretry import retry, error_jump\r\n\r\n\r\ndef error(s):\r\n    print('error1')\r\n\r\n\r\ndef error2(s):\r\n    print('error2')\r\n\r\n\r\ndef success(res):\r\n    print('success', res)\r\n\r\n\r\n@error_jump(on_exceptions=FileExistsError, error_callback=error)\r\n@error_jump(on_exceptions=FileNotFoundError, error_callback=error2)\r\n@error_jump(callback=success)\r\ndef run(s):\r\n    raise FileExistsError('1')\r\n\r\n\r\nif __name__ == '__main__':\r\n    run(2)\r\n```\r\n",
    "bugtrack_url": null,
    "license": "MIT Licence",
    "summary": "\u4e00\u4e2a\u9519\u8bef\u91cd\u8bd5\u88c5\u9970\u5668",
    "version": "2.0.6",
    "project_urls": {
        "Homepage": "https://github.com/Leviathangk/gretry"
    },
    "split_keywords": [
        "retry",
        "retrying"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "8d4a888f76719b87c2843a32f621e6be5105d7c24842a3597b9acb4d18c0ca80",
                "md5": "d727f92c61b279914ed7df000723b95c",
                "sha256": "4243fb3de0d3f43ff7b3a7c92a6c1afcc87bfd13cf0929f58c87bec537610f11"
            },
            "downloads": -1,
            "filename": "gretry-2.0.6.tar.gz",
            "has_sig": false,
            "md5_digest": "d727f92c61b279914ed7df000723b95c",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 4353,
            "upload_time": "2023-09-15T13:05:47",
            "upload_time_iso_8601": "2023-09-15T13:05:47.920938Z",
            "url": "https://files.pythonhosted.org/packages/8d/4a/888f76719b87c2843a32f621e6be5105d7c24842a3597b9acb4d18c0ca80/gretry-2.0.6.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-09-15 13:05:47",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "Leviathangk",
    "github_project": "gretry",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "gretry"
}
        
Elapsed time: 0.11164s