要从Python脚本启动程序,我使用以下方法:
def execute(command):
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
output = process.communicate()[0]
exitCode = process.returncode
if (exitCode == 0):
return output
else:
raise ProcessException(command, exitCode, output)
因此,当我启动一个像Process.execute("mvn clean install")
这样的进程时,我的程序会等到进程完成,然后才得到程序的完整输出。如果我正在运行一个需要一段时间才能完成的进程,这很烦人
我可以让我的程序通过在循环或其他过程结束之前轮询过程输出,逐行写入过程输出吗
我找到了this篇可能相关的文章
好的,我通过使用这个问题的一个片段Intercepting stdout of a subprocess while it is running在没有线程的情况下解决了这个问题(任何关于为什么使用线程会更好的建议都会受到赞赏)
只要命令输出行,您就可以使用iter处理这些行:
lines = iter(fd.readline, "")
。下面是一个完整的示例,展示了一个典型的用例(感谢@jfs的帮助):要在Python 3中刷新子进程的标准输出缓冲区后立即逐行打印子进程的输出,请执行以下操作:
注意:您不需要
p.poll()
——循环在达到eof时结束。而且您不需要iter(p.stdout.readline, '')
——预读错误在Python3中已经修复另见,Python: read streaming input from subprocess.communicate()
相关问题 更多 >
编程相关推荐