美文网首页
python的retrying库处理尝试多次请求

python的retrying库处理尝试多次请求

作者: 玉盘珍羞 | 来源:发表于2018-11-05 09:47 被阅读0次


retrying是一个python的重试包,可以用来自动重试一些可能运行失败的程序段,retrying提供一个装饰器函数retry,被装饰的函数就会在运行失败的情况下重新执行,默认只要一直报错就会不断重试。

参数:

  • stop_max_attempt_number:用来设定最大的尝试次数,超过该次数就会停止

  • stop_max_delay:从被装饰的函数开始执行的时间点开始到函数成功运行结束或失败报错中止的时间点。单位:毫秒

  • wait_fixed:设置在两次retrying之间的停留时间

  • retry_on_exception:指定出现哪些异常的时候再去retry 例:* retry_on_exception(retry_if_io_error)

  • retry_on_result:指定要在得到哪些结果再去retry
    retrying是一个python的重试包,可以用来自动重试一些可能运行失败的程序段,retrying提供一个装饰器函数retry,被装饰的函数就会在运行失败的情况下重新执行,默认只要一直报错就会不断重试。

  • stop_max_attempt_number:用来设定最大的尝试次数,超过该次数就会停止

  • stop_max_delay:从被装饰的函数开始执行的时间点开始到函数成功运行结束或失败报错中止的时间点。单位:毫秒

  • wait_fixed:设置在两次retrying之间的停留时间

  • retry_on_exception:指定出现哪些异常的时候再去retry
    例:retry_on_exception(retry_if_io_error)

  • retry_on_result:指定要在得到哪些结果再去retry
    例:retry_on_result(retry_if_result_none)

功能:

  • 一般装饰器api
  • 特定的停止条件(限制尝试次数)
  • 特定的等待条件(每次尝试之间的指数增长的时间等待)
  • 自定义的异常进行尝试
  • 自定义的异常进行尝试返回结果
  • 最简单的一个使用方法是无论有任何异常出现,都会一直重新调用一个函数、方法,直到返回一个值
import random
from retrying import retry

@retry
def do_something_unreliable():
    if random.randint(0, 10) > 1:
        print "just have a test"
        raise IOError("Broken sauce, everything is hosed!!!111one")
    else:
        return "Awesome sauce!"

print do_something_unreliable()

运行该段代码,你会发现每次随机打印的“just have a test”这句话次数不一致
例子
正如你上边看到的例子,默认的行为会一直重试,没有时间等待

@retry
def never_give_up_never_surrender():
    print "Retry forever ignoring Exceptions, don't wait between retries"
  • 骚年,不要太固执,加点限制,放弃之前,尝试几次(代码尝试几次后停止)
@retry(stop_max_attempt_number=7)
def stop_after_7_attempts():
    print "Stopping after 7 attempts"
  • 我们没有太多的时间,所以在每个尝试需要加个时间限制(多少s后停止尝试)
@retry(stop_max_delay=10000)
def stop_after_10_s():
    print "Stopping after 10 seconds"
  • 大多数事情并不是需要尽可能快的执行,所以加一些时间等待(每个尝试间加固定时间等待)
@retry(wait_fixed=2000)
def wait_2_s():
    print "Wait 2 second between retries"
  • 一些最好是随机的时间等待(每个尝试随机时间等待)
@retry(wait_random_min=1000, wait_random_max=2000)
def wait_random_1_to_2_s():
    print "Randomly wait 1 to 2 seconds between retries"
  • 再一次,在重新尝试分布式服务和其他远程端点时,很难击败指数级回退(不会翻译,大概就是每次尝试的等待时间以指数形式增长)
@retry(wait_exponential_multiplier=1000, wait_exponential_max=10000)
def wait_exponential_1000():
    print "Wait 2^x * 1000 milliseconds between each retry, up to 10 seconds, then 10 seconds afterwards"

我们有一些处理重新尝试的选项,它们会引起特定的或一般的异常,就像这里的情况一样(根据异常重试)

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 "Retry forever with no wait if an IOError occurs, raise any other errors"

@retry(retry_on_exception=retry_if_io_error, wrap_exception=True)
def only_raise_retry_error_when_not_io_error():
    print "Retry forever with no wait if an IOError occurs, raise any other errors 

我们也可以使用函数的结果来改变重新尝试的行为

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 "Retry forever ignoring Exceptions with no wait if return value is None"

任何停止、等待等的组合也会被支持,使你可以自由地混合和匹配。骚男,尝试起来吧!
原文:https://pypi.org/project/retrying/

相关文章

  • python的retrying库处理尝试多次请求

    retrying是一个python的重试包,可以用来自动重试一些可能运行失败的程序段,retrying提供一个装饰...

  • retrying库进行多次尝试

    最近写爬虫发现了一个非常方便的工具retrying,这个库的主要功能是对同一代码段进行多次尝试,常用于爬虫,如下为...

  • http retry

    Retrying http 请求时候的retry, 当失败时候需要进行重试。而 在python中有 retry 库...

  • retrying处理Python异常

    retrying处理Python异常 https://mp.weixin.qq.com/s/j9vqFQxVIKg...

  • 2019-04-25

    python内置的http请求库 urllib.request 请求模块 urlib.error 异常处理...

  • 对urllib 的理解

    它是python内置的http的请求库,urrlib.request 请求模块urllib.error 异常处理模...

  • python使用retrying重试请求

    当我们用 request 发起网络请求,时不时会遇到超时,当然不可能让这个请求一直阻塞,一般会设置一个超时时间,用...

  • 常用内置函数三

    一、Python的标准Web库 urllib是基于http高层的库 request处理客户端请求 response...

  • 02 urllib库的使用

    02 urllib库的使用 一、urllib库 1、概念 urllib 是一个用来处理网络请求的python标准库...

  • Python接口测试- requests 介绍

    Requests 库是一个优雅而简单的 Python HTTP 库,主要用于发送和处理 HTTP 请求。底层封装了...

网友评论

      本文标题:python的retrying库处理尝试多次请求

      本文链接:https://www.haomeiwen.com/subject/nesztqtx.html