I want to repeatedly send requests to process standard input and receive responses from standard output without calling subprocess
mul
You need to use communicate
to interact with your sub-process (https://docs.python.org/2/library/subprocess.html#subprocess.Popen.communicate). Here is an updated version of your main code:
import subprocess
p = subprocess.Popen(['python','subproc.py'],stdin=subprocess.PIPE,stdout=subprocess.PIPE)
while True:
s = raw_input('Enter message:')
response, _ = p.communicate(s)
if response!= '':
print "Process response:", response
else:
break
You also have to be careful that while you have a loop in your main code your subproc code only runs once. Only the first iteration will correctly receive a response the second communicate
call will raise an exception as the stdin
file of the subprocess will be closed by then.