-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtime_out_util.py
More file actions
33 lines (27 loc) · 979 Bytes
/
Copy pathtime_out_util.py
File metadata and controls
33 lines (27 loc) · 979 Bytes
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
import shlex
import subprocess
from threading import Timer
def run_with_timeout(cmd, timeout_sec, env=None):
global is_time_out
is_time_out = False
if env is None:
proc = subprocess.Popen(shlex.split(cmd), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
else:
proc = subprocess.Popen(shlex.split(cmd), stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env)
def time_out_call_back(p):
print 'time out need to kill'
global is_time_out
is_time_out = True
p.kill()
timer = Timer(timeout_sec, time_out_call_back, [proc])
try:
timer.start()
stdout, stderr = proc.communicate()
print str(stderr).replace('\n', '')
finally:
timer.cancel()
# print is_time_out
return is_time_out, str(stderr).replace('\n', ''), str(stdout)
if __name__ == '__main__':
tle_flag, info_err, info = run_with_timeout('sleep 20', timeout_sec=5)
print tle_flag, info_err, info