hello i have question in Python subprocess module
I want to interaction with child process (ex: child's stdin, stdout)
This is child Program's code
1 #include <stdio.h>
2
3 int main(){
4 char buf[1024];
5 printf("asdfsadfasff\n");
6 scanf("%s",buf);
7 printf("%s\n",buf);
8 }
this code is very simple, print the string, wait input, and print input string
and next, the python code is below
1 #!/usr/bin/python
2 from subprocess import *
3
4 fd = Popen(["./random"],stdin=PIPE,stdout=PIPE)
5 print "pid = %d"%(fd.pid) # print pid
6 result = ""
7 result = fd.stdout.read(-1) # read
8 print result
9 fd.stdin.write("write write write!!!\n")
10 result = fd.stdout.read(-1)
11 print result
in this case, I expected this program will work well, but in line 7 (fd.stdout.read(-1) was blocked and never working
I changed read function's param (read(-1), read(1), read(), read(1024)) but everything do not work
But, when i give string in stdin first, program worked.
I think child's stdout buffer was not fill when the program is ended... this is just my opnion
Is there any solution about this problem?
Edit1. when I execute program which is print string first, and wait user's input such as "sudo su" This worked well that I expected
I cannot understand why this case work well
1 from subprocess import *
2
3 fd = Popen(["sudo","su"],stdin=PIPE,stdout=PIPE)
4
5 print fd.stdout.read(-1)
6 print fd.stdin.write("asdfasdfas\n")