def execute(cmd, fin, fout, ferr, exclusive=False, single_thread=False):
fi = sp.PIPE
if fin != '':
fi = open(fin, 'r')
fo = open(fout, 'w')
fe = open(ferr, 'w')
if exclusive and single_thread: # requires root
cmd = 'chrt -f 99 ' + cmd
if single_thread: # bind to cpu #3 by default
cmd = 'taskset 1 ' + cmd
cmd = '/usr/bin/time -p -o _time_ ' + cmd
global proc
proc = sp.Popen(shlex.split(cmd), stdin=fi, stdout=fo, stderr=fe, env=os.environ.copy())
tm = time.time()
try:
ru = os.wait4(proc.pid, 0)
except:
raise
finally:
tm = time.time() - tm
proc = None
if fi != sp.PIPE:
fi.close()
fo.close()
fe.close()
#if ru[1] != 0:
# raise Exception('Command "{}" failed with exit code {}'.format(cmd, ru[1])
ru = list(ru) + [float(tm), [], cmd]
with open('_time_') as ft:
for l in ft:
l = l.strip().split()
try:
t = (l[0], float(l[1]))
ru[-2].append(t)
except ValueError:
pass
os.remove('_time_')
return ru
评论列表
文章目录