我同时运行两个子流程,并将两者的输出保存到单个日志文件中。 我还建立了一个超时来处理挂起的子进程。 当输出太大时,总是触发超时,并且任何一个子进程的stdout都不会保存到日志文件中。 上面Alex提出的答案并不能解决。
# Currently open log file.
log = None
# If we send stdout to subprocess.PIPE, the tests with lots of output fill up the pipe and
# make the script hang. So, write the subprocess's stdout directly to the log file.
def run(cmd, logfile):
#print os.getcwd()
#print ("Running test: %s" % cmd)
global log
p = subprocess.Popen(cmd, shell=True, universal_newlines = True, stderr=subprocess.STDOUT, stdout=logfile)
log = logfile
return p
# To make a subprocess capable of timing out
class Alarm(Exception):
pass
def alarm_handler(signum, frame):
log.flush()
raise Alarm
####
## This function runs a given command with the given fl