forked from zhanghe06/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_timeout.py
More file actions
74 lines (56 loc) · 1.16 KB
/
test_timeout.py
File metadata and controls
74 lines (56 loc) · 1.16 KB
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
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env python
# encoding: utf-8
"""
@author: zhanghe
@software: PyCharm
@file: DDDD.py
@time: 2017/6/6 下午1:54
"""
import time
# import ipdb
import inspect
import signal
# Define signal handler function
def myHandler(signum, frame):
print("Now, it's the time")
print signum, frame
# ipdb.set_trace()
inspect.getframeinfo(frame)
raise Exception('Function TimeOut!')
def with_time_out(s=10):
"""
函数超时装饰器
:param s:
:return:
"""
signal.signal(signal.SIGALRM, myHandler)
signal.alarm(s)
def decorator(func):
def wrapper(*args, **kw):
return func(*args, **kw)
return wrapper
return decorator
def set_timeout():
signal.signal(signal.SIGALRM, myHandler)
signal.alarm(1)
@with_time_out(1)
def a():
print 'a'
b()
def b():
time.sleep(10)
print 'b'
# # register signal.SIGALRM's handler
# signal.signal(signal.SIGALRM, myHandler)
# signal.alarm(2)
# while True:
# # time.sleep(3)
# try:
# a()
# except Exception as e:
# print 'get err:', e.message
# # print('not yet')
try:
a()
except Exception as e:
print e