【发布时间】:2011-03-09 14:02:52
【问题描述】:
同样的问题。
原因是 - 阅读以下内容后我仍然无法使其工作:
- Real-time intercepting of stdout from another process in Python
- Intercepting stdout of a subprocess while it is running
- How do I get 'real-time' information back from a subprocess.Popen in python (2.5)
- catching stdout in realtime from subprocess
我的情况是我有一个用 C 编写的控制台应用程序,让我们以循环中的代码为例:
tmp = 0.0;
printf("\ninput>>");
scanf_s("%f",&tmp);
printf ("\ninput was: %f",tmp);
它不断地读取一些输入并写入一些输出。
我与之交互的python代码如下:
p=subprocess.Popen([path],stdout=subprocess.PIPE,stdin=subprocess.PIPE)
p.stdin.write('12345\n')
for line in p.stdout:
print(">>> " + str(line.rstrip()))
p.stdout.flush()
到目前为止,每当我阅读表单 p.stdout 时,它总是等到进程终止,然后输出一个空字符串。我已经尝试了很多东西 - 但仍然是相同的结果。
我尝试了 Python 2.6 和 3.1,但版本无关紧要 - 我只需要让它在某个地方工作。
【问题讨论】:
-
我的情况完全相同,阅读了所有这些问题后,仍然没有发现任何优雅的工作。
-
我带着同样的问题来到这里,在这里找到了答案stackoverflow.com/q/375427/168034
标签: python subprocess stdout popen