上篇博文介绍了常见需要进行请求重试的场景,本篇博文试着剖析有名的python第三方库retrying源码。

在剖析其源码之前,有必要讲一下retrying的用法,方便理解。

安装:

pip install retrying

或者

easy_install retrying

一些用法实例如下:

#example 1
from retrying import retry @retry
def never_give_up_never_surrender():
print "一直重试且两次重试之间无需等待"
#example 2
from retrying import retry @retry(stop_max_attempt_number=7)
def stop_after_7_attempts():
print "重试七次后停止"
#example 3
from retrying import retry @retry(stop_max_delay=10000)
def stop_after_10_s():
print "十秒之后停止重试"
#example 4
from retrying import retry @retry(wait_fixed=2000)
def wait_2_s():
print "每次重试间隔两秒"
#example 5
from retrying import retry @retry(wait_random_min=1000, wait_random_max=2000)
def wait_random_1_to_2_s():
print "每次重试随机等待1到2秒"
#example 6
from retrying import retry @retry(wait_exponential_multiplier=1000, wait_exponential_max=10000)
def wait_exponential_1000():
print "指数退避,每次重试等待 2^x * 1000 毫秒,上限是10秒,达到上限后每次都等待10秒"
#example 7
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 "IO异常则重试,并且将其它异常抛出" @retry(retry_on_exception=retry_if_io_error, wrap_exception=True)
def only_raise_retry_error_when_not_io_error():
print "IO异常则重试,并且将其它异常用RetryError对象包裹"
#exampe 8,根据返回结果判断是否重试
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 "若返回结果为None则重试"

上面八个例子是retrying的用法,只需在要重试的方法上加上@retry注解,并以相应的条件为参数即可,那么@retry背后到底是如何实现的呢?下面给出@retry注解实现的方法。

 #装饰器模式,对需要重试的函数,利用retry注解返回
def retry(*dargs, **dkw):
"""
Decorator function that instantiates the Retrying object
@param *dargs: positional arguments passed to Retrying object
@param **dkw: keyword arguments passed to the Retrying object
"""
# support both @retry and @retry() as valid syntax
#当用法为@retry不带括号时走这条路径,dargs[0]为retry注解的函数,返回函数对象wrapped_f
if len(dargs) == 1 and callable(dargs[0]):
def wrap_simple(f): @six.wraps(f)#注解用于将函数f的签名复制到新函数wrapped_f
def wrapped_f(*args, **kw):
return Retrying().call(f, *args, **kw) return wrapped_f return wrap_simple(dargs[0]) else:#当用法为@retry()带括号时走这条路径,返回函数对象wrapped_f
def wrap(f): @six.wraps(f)#注解用于将函数f的签名复制到新函数wrapped_f
def wrapped_f(*args, **kw):
return Retrying(*dargs, **dkw).call(f, *args, **kw) return wrapped_f return wrap

当用@retry标记函数时,例如实例1,其实执行了

never_give_up_never_surrender = retry(never_give_up_never_surrender)

此时的never_give_up_never_surrender函数实际上是10-19行返回的wrapped_f函数,后续对never_give_up_never_surrender函数的调用都是调用的14行的wrapped_f函数。

当使用@retry()或者带参数的@retry(params)时,如实例2,实际执行了:

stop_after_7_attempts = retry(stop_max_attempt_number)(stop_after_7_attempts)

此时的stop_after_7_attempts函数实际上是22-29行的wrapped_f函数,后续对stop_after_7_attempts函数的调用都是对25行的wrapped_f函数调用。

可以看到实际上@retry将对需要重试的函数调用转化为对Retrying类中call函数的调用,重试逻辑也在这个函数实现,实现对逻辑代码的无侵入,代码如下:

 def call(self, fn, *args, **kwargs):
start_time = int(round(time.time() * 1000))
attempt_number = 1
while True:
#_before_attempts为@retry传进来的before_attempts,在每次调用函数前执行一些操作
if self._before_attempts:
self._before_attempts(attempt_number) try:#Attempt将函数执行结果或者异常信息以及执行次数作为内部状态,用True或False标记是内部存的值正常执行结果还是异常
attempt = Attempt(fn(*args, **kwargs), attempt_number, False)
except:
tb = sys.exc_info()#获取异常堆栈信息,sys.exc_info()返回type(异常类型), value(异常说明), traceback(traceback对象,包含更丰富的信息)
attempt = Attempt(tb, attempt_number, True) if not self.should_reject(attempt):#根据本次执行结果或异常类型判断是否应该停止
return attempt.get(self._wrap_exception) if self._after_attempts:#_after_attempts为@retry传进来的after_attempts,在每次调用函数后执行一些操作
self._after_attempts(attempt_number) delay_since_first_attempt_ms = int(round(time.time() * 1000)) - start_time
if self.stop(attempt_number, delay_since_first_attempt_ms):#根据重试次数和延迟判断是否应该停止
if not self._wrap_exception and attempt.has_exception:
# get() on an attempt with an exception should cause it to be raised, but raise just in case
raise attempt.get()
else:
raise RetryError(attempt)
else:#不停止则等待一定时间,延迟时间根据wait函数返回值和_wait_jitter_max计算
sleep = self.wait(attempt_number, delay_since_first_attempt_ms)
if self._wait_jitter_max:
jitter = random.random() * self._wait_jitter_max
sleep = sleep + max(0, jitter)
time.sleep(sleep / 1000.0) attempt_number += 1 #进行下一轮重试

9-13行将函数执行返回结果或异常存入Attempt对象attempt中,Attempt类如下:

class Attempt(object):
"""
An Attempt encapsulates a call to a target function that may end as a
normal return value from the function or an Exception depending on what
occurred during the execution.
"""
#value值为函数返回结果或异常,根据has_exception判断
def __init__(self, value, attempt_number, has_exception):
self.value = value
self.attempt_number = attempt_number
self.has_exception = has_exception
#返回函数执行结果或异常,并根据wrap_exception参数对异常用RetryError包裹
def get(self, wrap_exception=False):
"""
Return the return value of this Attempt instance or raise an Exception.
If wrap_exception is true, this Attempt is wrapped inside of a
RetryError before being raised.
"""
if self.has_exception:
if wrap_exception:
raise RetryError(self)
else:#重新构造原异常抛出
six.reraise(self.value[0], self.value[1], self.value[2])
else:
return self.value def __repr__(self):
if self.has_exception:
return "Attempts: {0}, Error:\n{1}".format(self.attempt_number, "".join(traceback.format_tb(self.value[2])))
else:
return "Attempts: {0}, Value: {1}".format(self.attempt_number, self.value)

15行根据should_reject函数的返回值判断是否停止重试,代码如下:

 def should_reject(self, attempt):
reject = False
#假如异常在retry_on_exception参数中返回True,则重试,默认不传异常参数时,发生异常一直重试
if attempt.has_exception:
reject |= self._retry_on_exception(attempt.value[1])
else:#假如函数返回结果在retry_on_result参数函数中为True,则重试
reject |= self._retry_on_result(attempt.value) return reject

22行根据重试次数和延迟判断是否应该停止重试,self.stop的赋值代码在构造函数中,代码片段如下:

        stop_funcs = []
if stop_max_attempt_number is not None:
stop_funcs.append(self.stop_after_attempt) if stop_max_delay is not None:
stop_funcs.append(self.stop_after_delay) if stop_func is not None:
self.stop = stop_func elif stop is None:#执行次数和延迟任何一个达到限制则停止
self.stop = lambda attempts, delay: any(f(attempts, delay) for f in stop_funcs) else:
self.stop = getattr(self, stop)
def stop_after_attempt(self, previous_attempt_number, delay_since_first_attempt_ms):
"""Stop after the previous attempt >= stop_max_attempt_number."""
return previous_attempt_number >= self._stop_max_attempt_number def stop_after_delay(self, previous_attempt_number, delay_since_first_attempt_ms):
"""Stop after the time from the first attempt >= stop_max_delay."""
return delay_since_first_attempt_ms >= self._stop_max_delay

29-33行等待一段时间再次重试,其中延迟时间重点是根据29行的wait函数计算,wait函数在构造函数中赋值,代码片段如下:

wait_funcs = [lambda *args, **kwargs: 0]
if wait_fixed is not None:
wait_funcs.append(self.fixed_sleep) if wait_random_min is not None or wait_random_max is not None:
wait_funcs.append(self.random_sleep) if wait_incrementing_start is not None or wait_incrementing_increment is not None:
wait_funcs.append(self.incrementing_sleep) if wait_exponential_multiplier is not None or wait_exponential_max is not None:
wait_funcs.append(self.exponential_sleep) if wait_func is not None:
self.wait = wait_func elif wait is None:#返回几个函数的最大值,作为等待时间
self.wait = lambda attempts, delay: max(f(attempts, delay) for f in wait_funcs) else:
self.wait = getattr(self, wait)

其中最值得研究的是指数退避延迟时间计算方法,函数为exponential_sleep,代码如下:

def exponential_sleep(self, previous_attempt_number, delay_since_first_attempt_ms):
exp = 2 ** previous_attempt_number
result = self._wait_exponential_multiplier * exp #延迟时间为_wait_exponential_multiplier*2^x
if result > self._wait_exponential_max:#假如大于退避上限_wait_exponential_max,则result为上限值
result = self._wait_exponential_max
if result < 0:
result = 0
return result

python重试库retryiny源码剖析的更多相关文章

  1. python部分重点底层源码剖析

    Python源码剖析—Set容器(hashtable实现) python源码剖析(内存管理和垃圾回收)

  2. 一个Python开源项目-腾讯哈勃沙箱源码剖析(上)

    前言 2019年来了,2020年还会远吗? 请把下一年的年终奖发一下,谢谢... 回顾逝去的2018年,最大的改变是从一名学生变成了一位工作者,不敢说自己多么的职业化,但是正在努力往那个方向走. 以前 ...

  3. python源码剖析学习记录-01

    学习<Python源码剖析-深度探索动态语言核心技术>教程         Python总体架构,运行流程   File Group: 1.Core Modules 内部模块,例如:imp ...

  4. 《python解释器源码剖析》第0章--python的架构与编译python

    本系列是以陈儒先生的<python源码剖析>为学习素材,所记录的学习内容.不同的是陈儒先生的<python源码剖析>所剖析的是python2.5,本系列对应的是python3. ...

  5. 【Python源码剖析】对象模型概述

    Python 是一门 面向对象 语言,实现了一个完整的面向对象体系,简洁而优雅. 与其他面向对象编程语言相比, Python 有自己独特的一面. 这让很多开发人员在学习 Python 时,多少有些无所 ...

  6. Python源码剖析|百度网盘免费下载|Python新手入门|Python新手学习资料

    百度网盘免费下载:Python源码剖析|新手免费领取下载 提取码:g78z 目录  · · · · · · 第0章 Python源码剖析——编译Python0.1 Python总体架构0.2 Pyth ...

  7. socket_server源码剖析、python作用域、IO多路复用

    本节内容: 课前准备知识: 函数嵌套函数的使用方法: 我们在使用函数嵌套函数的时候,是学习装饰器的时候,出现过,由一个函数返回值是一个函数体情况. 我们在使用函数嵌套函数的时候,最好也这么写. def ...

  8. Python 源码剖析(一)【python对象】

    处于研究python内存释放问题,在阅读部分python源码,顺便记录下所得.(基于<python源码剖析>(v2.4.1)与 python源码(v2.7.6)) 先列下总结:      ...

  9. Golang 源码剖析:log 标准库

    Golang 源码剖析:log 标准库 原文地址:Golang 源码剖析:log 标准库 日志 输出 2018/09/28 20:03:08 EDDYCJY Blog... 构成 [日期]<空格 ...

随机推荐

  1. Go的Get命令兼容公司Gitlab仓库的HTTP协议

    对于公司的私有Gitlab仓库,没有对https支持,在使用最新版本的go get命令时,需要使用-insecure参数来支持http,但如果导入的包里边依赖了需要https的仓库,就不好使了,折腾了 ...

  2. web网站在不同设备下进行缩放

    <script> function calculatescale() { var phoneWidth = parseInt(window.screen.width); var phone ...

  3. OC中NSString的使用、字符串的使用

    字符串作为OC语言的基本对象类型,被我们在各种地方广泛使用,因此熟悉掌握字符串的属性和方法,是OC必不可少的基础之一. 字符串分为可变字符串(NSMutableString)和不可变字符串(NSStr ...

  4. Python之路【第三篇】编码

    Python代码——>字节码——>机器码——>计算机 Windows: cmd ==> python 文件路径 cmd ==>python >> 输入命令 L ...

  5. beeline方式连接hive

    什么是beeline 常用操作 https://www.cnblogs.com/xinfang520/p/7684598.html

  6. 【转载】row cache lock

    转自:http://blog.itpub.net/26736162/viewspace-2139754/   定位的办法: --查询row cache lock等待 select event,p1   ...

  7. 新FSM的一些思路

    好久之前写过一篇关于状态机的小例子,可以看这里http://www.cnblogs.com/mawanli/p/5966080.html,这篇博客首先感谢需要感谢当时看到凉鞋的笔记博客, 凉鞋的博客地 ...

  8. linux下tomcat服务器的相关命令

    一:Linux下tomcat服务的启动.关闭与错误跟踪,使用PuTTy远程连接到服务器以后,通常通过以下几种方式启动关闭tomcat服务:切换到tomcat主目录下的bin目录(cd usr/loca ...

  9. 简单 v.s. 基础

    无论做平面设计还是做摄影创作,其基础都是一些比较粗浅的看似毫无用处的简单技能.例如画直线.拍挂在墙上的电视机,不一而足. 同样的现象还能在web的前端设计中看到.一堆类似小孩学绘画的标签,几个可以更改 ...

  10. vue组件推荐

    Vue 是一个轻巧.高性能.可组件化的MVVM库,API简洁明了,上手快.从Vue推出以来,得到众多Web开发者的认可.在公司的Web前端项目开发中,多个项目采用基于Vue的UI组件框架开发,并投入正 ...