这篇文章主要为大家展示了“如何解决python超时重新请求问题”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“如何解决python超时重新请求问题”这篇文章吧。
在应用中,有时候会 依赖第三方模块执行方法,比如调用某模块的上传下载,数据库查询等操作的时候,如果出现网络问题或其他问题,可能有超时重新请求的情况;
目前的解决方案有
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()
以上是“如何解决python超时重新请求问题”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。