Thanks for taking time to answer the question. I am playing around with Python 3.4 and I have two simple python programs. One, a program called test.py that takes a user input and prints something.
while True:
print("enter something...")
x = input()
print(x)
time.sleep(1)
To send input to this program, I have another program that uses subprocess:
from subprocess import Popen, PIPE
cat = Popen('python test.py', shell=True, stdin=PIPE, stdout=PIPE)
cat.stdin.write("hello, world!\n")
cat.stdin.flush()
print(cat.stdout.readline())
cat.stdin.write("and another line\n")
cat.stdin.flush()
print(cat.stdout.readline())
However when I run the above program, I get an error:
enter something...
hello, world!
Traceback (most recent call last):
File "/opt/test.py", line 9, in <module>
x = input()
EOFError: EOF when reading a line
Exception ignored in: <_io.TextIOWrapper name='<stdout>' mode='w' encoding='UTF-8'>
BrokenPipeError: [Errno 32] Broken pipe
And if I replace test.py with a standard linux command like 'cat', things work as expected.
Is there any way I send multiple stdin writes and read multiple stdout back?
Popen(['python', 'test.py'], shell=True, stdin=PIPE, stdout=PIPE)cat.stdin.write("hello, world!\n")should raise an error if you use Python 3 (universal_newlines=Trueis required to enable the text mode) therefore either your actual code is different or you are not using Python 3.importthe module and call its functions (usemultiprocessingif necessary) instead of running it as a subprocess