python-subprocess: Lesen-Ausgabe von Teilprozess

Ich habe folgenden Skript:

#!/usr/bin/python

while True:
    x = raw_input()
    print x[::-1]

Rufe ich es aus ipython:

In [5]: p = Popen('./script.py', stdin=PIPE)

In [6]: p.stdin.write('abc\n')
cba

und es funktioniert gut.

Jedoch, wenn ich dies tun:

In [7]: p = Popen('./script.py', stdin=PIPE, stdout=PIPE)

In [8]: p.stdin.write('abc\n')

In [9]: p.stdout.read()

den Dolmetscher hängt. Was mache ich falsch? Ich möchte in der Lage sein, sowohl das schreiben als auch das Lesen aus einem anderen Prozess mehrere Male, um einige Aufgaben zu diesem Prozess. Was muss ich anders machen?

BEARBEITEN 1

Wenn ich communicate, bekomme ich diese:

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)

EDIT 2

Ich versuchte Spülung:

#!/usr/bin/python

import sys

while True:
        x = raw_input()
        print x[::-1]
        sys.stdout.flush()

und hier:

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()

aber es hängt wieder.

InformationsquelleAutor gruszczy | 2010-09-27

Schreibe einen Kommentar