Read / Write Subprocess Popen ()

I am trying to talk to a child process using the python subprocess.Popen () call. In my real code, I implement the IPC type, so I want to write some data, read the answer, write some more data, read the answer, and so on. Because of this, I cannot use Popen.communicate (), which otherwise works well for a simple case.

This code shows my problem. He does not even receive the first answer, freezes at the first "reading result". What for? How can I do this work as I expect?

import subprocess p = subprocess.Popen(["sed", 's/a/x/g'], stdout = subprocess.PIPE, stdin = subprocess.PIPE) p.stdin.write("abc\n") print "Reading result:" print p.stdout.readline() p.stdin.write("cat\n") print "Reading result:" print p.stdout.readline() 
+7
source share
2 answers

I would try using Popen().communicate() if you can, since there are many nice things for you, but if you need to use Popen() exactly as you described, you will need to set sed to flush its buffer after new lines with the -l option:

 p = subprocess.Popen(['sed', '-l', 's/a/x/g'], stdout=subprocess.PIPE, stdin=subprocess.PIPE) 

and your code should work fine

+4
source

sed output is buffered and only outputs its data until a sufficient amount has been accumulated or the input stream has been exhausted.

Try the following:

 import subprocess p = subprocess.Popen(["sed", 's/a/x/g'], stdout = subprocess.PIPE, stdin = subprocess.PIPE) p.stdin.write("abc\n") p.stdin.write("cat\n") p.stdin.close() print "Reading result 1:" print p.stdout.readline() print "Reading result 2:" print p.stdout.readline() 

Keep in mind that this cannot be done reliably, which huge data causes stdin block once when the buffer is full. The best way to do this is communicate() .

+3
source

All Articles