ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

python 超时重试方法

2019-09-28 14:00:26  阅读:288  来源: 互联网

标签:__ retry python request 重试 time print 超时 def


在应用中,有时候会 依赖第三方模块执行方法,比如调用某模块的上传下载,数据库查询等操作的时候,如果出现网络问题或其他问题,可能有超时重新请求的情况;

目前的解决方案有

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

2.多线程,但是 如果是大量的数据重复操作尝试,会出现线程管理混乱,开启上万个线程的问题;

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

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

# -*- 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()

  参考:

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

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

retry: https://blog.csdn.net/lxy210781/article/details/95253026

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

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

 

标签:__,retry,python,request,重试,time,print,超时,def
来源: https://www.cnblogs.com/dasheng-maritime/p/11602921.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有