python,子进程:从子进程读取输出
我有以下脚本:
#!/usr/bin/python
while True:
x = raw_input()
print x[::-1]
我从 ipython
调用它:
In [5]: p = Popen('./script.py', stdin=PIPE)
In [6]: p.stdin.write('abc\n')
cba
它工作正常.
但是,当我这样做时:
In [7]: p = Popen('./script.py', stdin=PIPE, stdout=PIPE)
In [8]: p.stdin.write('abc\n')
In [9]: p.stdout.read()
解释器挂了.我究竟做错了什么?我希望能够多次写入和读取另一个进程,将一些任务传递给这个进程.我需要做什么不同的事情?
the interpreter hangs. What am I doing wrong? I would like to be able to both write and read from another process multiple times, to pass some tasks to this process. What do I need to do differently?
编辑 1
如果我使用 communicate
,我会得到这个:
If I use communicate
, I get this:
In [7]: p = Popen('./script.py', stdin=PIPE, stdout=PIPE)
In [8]: p.communicate('abc\n')
Traceback (most recent call last):
File "./script.py", line 4, in <module>
x = raw_input()
EOFError: EOF when reading a line
Out[8]: ('cba\n', None)
编辑 2
我试过冲洗:
#!/usr/bin/python
import sys
while True:
x = raw_input()
print x[::-1]
sys.stdout.flush()
这里:
In [5]: from subprocess import PIPE, Popen
In [6]: p = Popen('./script.py', stdin=PIPE, stdout=PIPE)
In [7]: p.stdin.write('abc')
In [8]: p.stdin.flush()
In [9]: p.stdout.read()
但它又挂了.
我认为这里有两个问题:
I believe there are two problems at work here:
1) 您的父脚本调用 p.stdout.read()
,它将读取所有数据,直到文件结束.但是,您的子脚本在无限循环中运行,因此文件结尾永远不会发生.可能你想要 p.stdout.readline()
?
1) Your parent script calls p.stdout.read()
, which will read all data until end-of-file. However, your child script runs in an infinite loop so end-of-file will never happen. Probably you want p.stdout.readline()
?
2) 在交互模式下,大多数程序一次只缓冲一行.当从另一个程序运行时,它们会缓冲更多.缓冲在很多情况下提高了效率,但是当两个程序需要交互通信时就会出现问题.
2) In interactive mode, most programs do buffer only one line at a time. When run from another program, they buffer much more. The buffering improves efficiency in many cases, but causes problems when two programs need to communicate interactively.
在p.stdin.write('abc\n')
之后添加:
p.stdin.flush()
在您的子进程脚本中,在 print x[::-1]
之后在循环中添加以下内容:
In your subprocess script, after print x[::-1]
add the following within the loop:
sys.stdout.flush()
(和 import sys
在顶部)