python 超时重试方法

在应用中,有时候会 依赖第三方模块执行方法,好比调用某模块的上传下载,数据库查询等操做的时候,若是出现网络问题或其余问题,可能有超时从新请求的状况;html

目前的解决方案有python

1. 信号量,但不支持window;数据库

2.多线程,可是 若是是大量的数据重复操做尝试,会出现线程管理混乱,开启上万个线程的问题;网络

3.结合采用 eventlet 和 retrying模块 (eventlet 原理尚需深刻研究)多线程

下面的方法实现:超过指定时间从新尝试某个方法dom

# -*- coding: utf-8 -*-
import random
import time

import eventlet
from retrying import retry

eventlet.monkey_patch()


class RetryTimeOutException(Exception):
    def __init__(self, *args, **kwargs):
        pass


def retry_if_timeout(exception):
    """Return True if we should retry (in this case when it's an IOError), False otherwise"""
    return isinstance(exception, RetryTimeOutException)


def retry_fun(retries=3, timeout_second=2):
    """
    will retry ${retries} times when process time beyond ${timeout_second} ;
    :param retries: The retry times
    :param timeout_second: The max process time
    """

    def retry_decor(func):
        @retry(stop_max_attempt_number=retries, retry_on_exception=retry_if_timeout)
        def decor(*args, **kwargs):
            print("In retry method..")
            pass_flag = False
            with eventlet.Timeout(timeout_second, False):
                r = func(*args, **kwargs)
                pass_flag = True
                print("Success after method.")
            if not pass_flag:
                raise RetryTimeOutException("Time out..")
            print("Exit from retry.")
            return r

        return decor

    return retry_decor


def do_request():
    print("begin request...")
    sleep_time = random.randint(1, 4)
    print("request sleep time: %s." % sleep_time)
    time.sleep(sleep_time)
    print("end request...")
    return True


@retry_fun(retries=3)
def retry_request():
    r = do_request()
    print(r)


if __name__ == '__main__':
    retry_request()

  参考:this

安装依赖模块:pip install retrying eventlet -i https://pypi.tuna.tsinghua.edu.cn/simple/.net

装饰器用法:https://blog.csdn.net/u013205877/article/details/78872278线程

retry: https://blog.csdn.net/lxy210781/article/details/952530263d

超时:https://blog.csdn.net/yuanpython/article/details/90522567

其余方法:https://www.cnblogs.com/lyxdw/p/10033118.html

相关文章
相关标签/搜索