【发布时间】:2014-04-04 10:04:03
【问题描述】:
我想通过 python 运行一个很长的过程(calculix 模拟)。
如here 所述,可以使用communicate() 读取控制台字符串。
据我了解,该过程完成后返回字符串?进程运行时是否有可能获得控制台输出?
【问题讨论】:
-
你的进程
calculix模拟运行时在控制台写入数据? -
是的,确保它写入控制台
我想通过 python 运行一个很长的过程(calculix 模拟)。
如here 所述,可以使用communicate() 读取控制台字符串。
据我了解,该过程完成后返回字符串?进程运行时是否有可能获得控制台输出?
【问题讨论】:
calculix模拟运行时在控制台写入数据?
您必须使用subprocess.Popen.poll 来检查进程是否终止。
while sub_process.poll() is None:
output_line = sub_process.stdout.readline()
这将为您提供运行时输出。
【讨论】:
这应该可行:
sp = subprocess.Popen([your args], stdout=subprocess.PIPE)
while sp.poll() is None: # sp.poll() returns None while subprocess is running
output = sp.stdout # here you have acccess to the stdout while the process is running
# Do stuff with stdout
注意我们这里没有在子进程上调用communicate()。
【讨论】: