子进程命令的实时输出
subprocess.Popen
subprocess.PIPE
是否有一种方法既可以存储输出(用于日志记录和错误检查),也可以生成实时流输出?
ret_val = subprocess.Popen( run_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True )output, errors = ret_val.communicate()log_file.write(output)print outputif( ret_val.returncode ): print "RUN failed\n\n%s\n\n" % (errors) success = Falseif( errors ): log_file.write("\n\n%s\n\n" % errors)
run_command
tee
ret_val = subprocess.Popen( run_command, stdout=log_file, stderr=subprocess.PIPE, shell=True )while not ret_val.poll(): log_file.flush()
tail -f log.txt
log_file = 'log.txt'
).
忽然笑
相关分类