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")
The child process
./randomis usingstdio, which by default does “full buffering” rather than “line buffering” when the process’s stdout is not a tty device. Since python runs the process via pipes (which are not tty devices) the program is buffering its output, and not printing it at the time of thescanfcall. The buffer is flushed (written to stdout) only (a) when it fills up; (b) when explicitly flushed with anfflushcall; or (c) at program exit.You can modify the child process to do explicit
fflushcalls, or you can use a pseudo-tty from Python, e.g., withpexpectorsh. (Note that theshmodule by default uses a pty for output only but this is usually sufficient.) Or, in cases like this where the program you’re running only “looks interactive” (but is actually totally predictable), you can just run it with the known input sequence pre-provided:although then of course you’ll get all the stdout input mixed together, and have to separate out the two lines it printed (in this particular case).