跳转到主要内容

Python可调用重试工具。

项目描述

简介

Redo提供多种方法,将重试能力无缝添加到任何Python可调用函数中。Redo包括普通函数(《redo.retry》,《redo.retry_async》),装饰器(《redo.retriable》,《redo.retriable_async》)和上下文管理器(《redo.retrying》),以便您以最佳方式将其集成到项目中。作为额外的好处,还包括一个独立的接口(《retry》)。

安装

使用pip安装,请运行以下命令

pip install redo

如何使用

以下是可用函数的列表

  • retrier

  • retry

  • retry_async

  • retriable

  • 可重试的异步操作

  • 重试(上下文管理器)

重试器(attempts=5, sleeptime=10, max_sleeptime=300, sleepscale=1.5, jitter=1)

一个在重试之间休眠的生成器函数,处理指数退避和抖动。您正在重试的操作应在retrier产生后运行。在每次迭代中,我们休眠的时间为sleeptime + random.randint(-jitter, jitter)。之后,sleeptime乘以sleepscale以用于下一次迭代。

参数详情

  1. attempts (int): 最大尝试次数;默认为5

  2. sleeptime (float): 两次尝试之间的睡眠秒数;默认为60秒(一分钟)

  3. max_sleeptime (float): 最长睡眠时间,以秒为单位;默认为300秒(五分钟)

  4. sleepscale (float): 每次迭代乘以睡眠时间的倍数;默认为1.5

  5. jitter (int): 在每次迭代中引入睡眠时间的随机抖动。该值在[-jitter, +jitter]之间随机选择。默认为1

输出: None,最多尝试attempts次数

示例

>>> n = 0
>>> for _ in retrier(sleeptime=0, jitter=0):
...     if n == 3:
...         # We did the thing!
...         break
...     n += 1
>>> n
3
>>> n = 0
>>> for _ in retrier(sleeptime=0, jitter=0):
...     if n == 6:
...         # We did the thing!
...         break
...     n += 1
... else:
...     print("max tries hit")
max tries hit

retry(action, attempts=5, sleeptime=60, max_sleeptime=5 * 60, sleepscale=1.5, jitter=1, retry_exceptions=(Exception,), cleanup=None, args=(), kwargs={})

调用操作函数,直到成功或放弃。

参数详情

  1. action (callable): 要重试的函数

  2. attempts (int): 最大尝试次数;默认为5

  3. sleeptime (float): 两次尝试之间的睡眠秒数;默认为60秒(一分钟)

  4. max_sleeptime (float): 最长睡眠时间,以秒为单位;默认为300秒(五分钟)

  5. sleepscale (float): 每次迭代乘以睡眠时间的倍数;默认为1.5

  6. jitter (int): 在每次迭代中引入睡眠时间的随机抖动。该值在[-jitter, +jitter]之间随机选择。默认为1

  7. retry_exceptions (tuple): 要捕获的异常元组。如果action()抛出其他异常,则立即重新抛给调用者。

  8. cleanup (callable): 可选;在捕获到retry_exceptions之一时调用。不向清理函数传递任何参数;如果您的清理需要参数,请考虑使用functools.partial或lambda函数。

  9. args (tuple): 调用action时的位置参数

  10. kwargs (dict): 调用action时的关键字参数

输出: action(*args, **kwargs)返回的任何内容

输出: action(*args, **kwargs)抛出的任何异常。在最后一次尝试之前捕获retry_exceptions,在这种情况下将重新抛出。

示例

>>> count = 0
>>> def foo():
...     global count
...     count += 1
...     print(count)
...     if count < 3:
...         raise ValueError("count is too small!")
...     return "success!"
>>> retry(foo, sleeptime=0, jitter=0)
1
2
3
'success!'

retry_async(func, attempts=5, sleeptime_callback=calculate_sleep_time, retry_exceptions=Exception, args=(), kwargs={}, sleeptime_kwargs=None)

一个异步函数,用于重试给定的异步可调用对象。

参数详情

  1. func (function): 要重试的可等待函数

  2. attempts (int): 最大尝试次数;默认为5

  3. sleeptime_callback (function): 确定每次尝试后睡眠时间的函数;默认为calculateSleepTime

  4. retry_exceptions (list or exception): 要重试的异常;默认为Exception

  5. args (list): 要传递给func的参数

  6. kwargs (dict): 要传递给func的关键字参数

  7. sleeptime_kwargs (dict): 要传递给sleeptime_callback的关键字参数

输出: 成功调用func的值或超过尝试次数后抛出异常。

示例

>>> async def async_action():
...     # Your async code here
>>> result = await retry_async(async_action)

retriable(*retry_args, **retry_kwargs)

retry()创建装饰器工厂。将函数包裹在@retriable(...)中以赋予其重试能力!

参数详情:retry相同,但除了留出actionargskwargs(它们留到正常的函数定义中)之外。

输出: 函数装饰器

示例

>>> count = 0
>>> @retriable(sleeptime=0, jitter=0)
... def foo():
...     global count
...     count += 1
...     print(count)
...     if count < 3:
...         raise ValueError("count too small")
...     return "success!"
>>> foo()
1
2
3
'success!'

retriable_async(retry_exceptions=Exception, sleeptime_kwargs=None)

异步重试函数的装饰器。

参数详情

  1. retry_exceptions (list or exception): 要重试的异常;默认为Exception

  2. sleeptime_kwargs (dict): 传递给睡眠时间回调的关键字参数

输出: 应用retry_async到装饰函数的函数装饰器。

示例

>>> @retriable_async()
... async def async_action():
...     # Your async code here
>>> result = await async_action()

retrying(func, *retry_args, **retry_kwargs)

一个用于包装具有重试功能的函数的上下文管理器。

参数详情

  1. func (可调用对象): 要包装的函数,根据 retry 传递其他参数

输出:__enter__ 时返回 retriable(func) 的上下文管理器

示例

>>> count = 0
>>> def foo():
...     global count
...     count += 1
...     print(count)
...     if count < 3:
...         raise ValueError("count too small")
...     return "success!"
>>> with retrying(foo, sleeptime=0, jitter=0) as f:
...     f()
1
2
3
'success!'

由以下支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页面