#coding=utf-8
import os
def run_cmd(ut):
#print(ut)
val = os.system(ut)
print (val)
#"msg":"OK","data"
def call_cmd(ut):
str = os.popen(ut).read()
a = str.split("\n")
ret = ''
for b in a:
print (b)
end = b.find('"msg":"OK"')
#print('end=',end)
if end > 0 and ret != 'OK':
if end > 0:
ret = 'OK'
else:
start = b.find('"msg":"')
ret = b[start:start+20]
return ret
ut_list = []
ut_list.append("python ut1.py")
ut_list.append("python ut2.py")
ut_list.append("python ut3.py")
ut_result = []
for x in ut_list:
#print(x)
ret = call_cmd(x)
ut_result.append(ret)
print('===========results===========')
index = 0
for x in ut_result:
print(ut_list[index],'\t', x)
index += 1
结果
===========results===========
python ut1.py OK
python ut2.py OK
python ut3.py Error
version 2 多线程版本
#coding=utf-8
import os
import threading
import time
#"msg":"OK","data"
def call_cmd(index, ut):
str = os.popen(ut).read()
a = str.split("\n")
ret = ''
for b in a:
#print (b)
end = b.find('"msg":"OK"')
#print('end=',end)
if end > 0 and ret != 'OK':
if end > 0:
ret = 'OK'
else:
start = b.find('"msg":"')
ret = b[start:start+20]
ut_result[index] = ret
return ret
start = time.time()
ut_list = []
ut_list.append("python ut1.py")
ut_list.append("python ut2.py")
ut_list.append("python ut3.py")
ut_result = []
threads = []
index = 0
for x in ut_list:
print(x, 'is working...')
ret = 'Error'
ut_result.append(ret)
t1 = threading.Thread(target=call_cmd, args=(index, x, ))
t1.start()
threads.append(t1)
index += 1
for t in threads:
t.join()
print('===========results===========')
index = 0
for x in ut_result:
print(ut_list[index],'\t', x)
index += 1
end = time.time()
cost_time = int((end - start) * 1000)
print("--- ut all cost", cost_time, "milliseconds ---")
代码#coding=utf-8import osdef run_cmd(ut): #print(ut) val = os.system(ut) print (val)#"msg":"OK","data"def call_cmd(ut): str = os.popen(ut).read() a = str.split("\n") ret = '' for b in a: print (b) end ..