工具代码-timeout-定时器

工具代码-timeout

完成timeout,对于一个自动化用例实现超时后自动停止

 

 1 import time。
 2 #from threading import Thread
 3 import threading
 4 import sys
 5 
 6 class KThread(threading.Thread):
 7     """A subclass of threading.Thread, with a kill()
 8     method.
 9     
10     Come from:
11     Kill a thread in Python: 
12     http://mail.python.org/pipermail/python-list/2004-May/260937.html
13     """
14     def __init__(self, *args, **kwargs):
15         threading.Thread.__init__(self, *args, **kwargs)
16         self.killed = False
17 
18     def start(self):
19         """Start the thread."""
20         self.__run_backup = self.run
21         self.run = self.__run      # Force the Thread to install our trace.
22         threading.Thread.start(self)
23 
24     def __run(self):
25         """Hacked run function, which installs the
26         trace."""
27         sys.settrace(self.globaltrace)
28         self.__run_backup()
29         self.run = self.__run_backup
30 
31     def globaltrace(self, frame, why, arg):
32         if why == 'call':
33           return self.localtrace
34         else:
35           return None
36 
37     def localtrace(self, frame, why, arg):
38         if self.killed:
39           if why == 'line':
40             raise SystemExit()
41         return self.localtrace
42 
43     def kill(self):
44         self.killed = True
45 
46 class Timeout(Exception):
47     """function run timeout"""
48     
49 def timeout(seconds = 3):
50     def timeout_decorator(func):        
51         def _new_func(oldfunc, result, oldfunc_args, oldfunc_kwargs):
52             result.append(oldfunc(*oldfunc_args, **oldfunc_kwargs))
53         
54         def _(*args, **kwargs):
55             result = []
56             new_kwargs = { # create new args for _new_func, because we want to get the func return val to result list
57                 'oldfunc': func,
58                 'result': result,
59                 'oldfunc_args': args,
60                 'oldfunc_kwargs': kwargs
61             }
62             thd = KThread(target=_new_func, args=(), kwargs=new_kwargs)
63             thd.setDaemon(True)
64             thd.start()
65             thd.join(seconds)
66             #alive = thd.isAlive()
67             alive = thd.is_alive()
68             thd.kill()
69             try:
70                 if alive:
71                     raise Timeout(u'function run too long, timeout %d seconds.' % seconds)
72                 else:
73                     if len(result) == 0:
74                         raise Exception("TESTCASE FAILED")
75                     else:
76                         return result[0]
77             except Exception as e:
78                 print("toolong",e)
79         _.__name__ = func.__name__
80         _.__doc__ = func.__doc__
81         return _
82     return timeout_decorator
83 
84 @timeout(3)
85 def fun():
86     print("111231111111---")
87     time.sleep(100)
88     print("===")
89 @timeout(3)
90 def fun0():
91     print("11111")
92     time.sleep(2)
93     print("end")
94     
95 
96 fun()
97 fun0()

 多线程-定时器

posted on 2020-12-03 09:28  lexn  阅读(181)  评论(0编辑  收藏  举报

导航