python 超時重試方法


python 超時重試方法

 

在應用中,有時候會 依賴第三方模塊執行方法,比如調用某模塊的上傳下載,數據庫查詢等操作的時候,如果出現網絡問題或其他問題,可能有超時重新請求的情況;

目前的解決方案有

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

2.多線程,但是 如果是大量的數據重復操作嘗試,會出現線程管理混亂,開啟上萬個線程的問題;

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

下面的方法實現:超過指定時間重新嘗試某個方法

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# -*- 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

 

 
 
標簽: python
0
0
 
 
 
« 上一篇: c++動態編譯 並調用
» 下一篇: python 超時重試的方法 signal手段
posted @ 2019-09-28 13:56  百變小超  閱讀( 158)  評論( 1編輯  收藏


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM