I’m having problems reading/writing to stdin/stdout in a child process with subprocess.communicate().
This is the child process (in C)
#include <stdio.h>
int main ()
{
char buf[128];
printf("test\n");
gets(buf);
printf("%s", buf);
return 0;
}
This is the program in python that calls that child program
import subprocess
proc = subprocess.Popen('./test', stdin=subprocess.PIPE, stdout=subprocess.PIPE)
stdin, stderr = proc.communicate()
print stdin
proc.communicate(stdin)
stdin, stderr = proc.communicate()
print stdin
print stdin
I would expect this to print
test
input was test:
However, it seems that proc.communicate() causes an EOF for gets(), causing the child application to terminate. Is there anyway I can communicate with the child application without sending an EOF? IE I would like to read to the application AND THEN write to it.
From the documentation for
Popen.communicate():This means that you can only call
communicate()once for your subprocess.Getting real time output can be kind of tricky, but in your example it shouldn’t be too difficult since you are only attempting to read a single line. The following should work:
Note that I renamed some of your variables, since the output from your subprocess is not really stdin for your Python process.