-
Notifications
You must be signed in to change notification settings - Fork 8
/
timeout.py
62 lines (52 loc) · 1.23 KB
/
timeout.py
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'timeout function'
# import multiprocessing
import time
import threading
import socket
def host_check(dn):
try:
socket.gethostbyname(dn)
print 'true'
return True
except socket.error:
print 'false'
return False
def time_out(func, args):
p = threading.Thread(target=func, args=[args])
p.setDaemon(True)
p.start()
p.join(1)
if p.is_alive():
print 'timeout'
return False
if __name__ == '__main__':
t2 = 0
t00 = time.time()
dn = '06.com'
def timeout(func, args):
global t2
p = threading.Thread(target=func, args=[args])
p.setDaemon(True)
# p = multiprocessing.Process(target=host_check('06.com'))
t0 = time.time()
p.start()
t1 = time.time()
p.join(1)
t2 = time.time()
print t0 - t00
print t1 - t0
print t2 - t1
# print 'test'
if p.is_alive():
# print 'timeout'
# p.terminate()
print 'timeout'
return False
t4 = time.time()
print t4 - t2
# return True
print timeout(host_check,'dd.de')
t3 = time.time()
print t3 - t2