要从我的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”),我的程序等待,直到过程完成,只有这样,我得到我的程序的完整输出。这是恼人的,如果我运行一个过程需要一段时间才能完成。
我可以让我的程序逐行写入过程输出,通过在循环结束之前轮询过程输出或什么?
*[编辑]
对不起,我没有搜索很好,在发布此问题。线程实际上是关键。找到一个例子,这里显示如何做:*
Python Subprocess.Popen from a thread