views:

37

answers:

1

Hi, I need to interface a C console program (as subprocess) with Python using stdin/stdout.

the C program is more o less it:

    tmp = 0.0;  
    printf("\ninput>>");
    scanf_s("%f",&tmp);
    printf ("\ninput was: %f",tmp);

    tmp = 0.0;
    printf("\ninput>>");
    scanf_s("%f",&tmp);
    printf ("\ninput was: %f",tmp);

    tmp = 0.0;
    printf("\ninput>>");
    scanf_s("%f",&tmp);
    printf ("\ninput was: %f",tmp);

Using python subprocess module I need to read data from this program, the write something, then read again and so on. I used the following code:

>>> p=subprocess.Popen(['C:\T.exe'],stdout=subprocess.PIPE,stdin=subprocess.PIPE)
>>> o,i=communicate('123\n')

the output of o is:

input>>
input was: 123.000000
input>>
input was: 0.000000
input>>
input was: 0.000000

I would expect the subprocess to wait on input until another o,i=communicate() call. Why it is proceding to the end of the program without any input? how to fix it?

+3  A: 

There can be at most one call to communicate() for each process, because communicate() waits for the child process to terminate. To repeatedly read and write from/to a process's standard streams, use the stdout and stdin attributes of the Popen class.

Philipp
if i use p.stdout.read(10) then the python shell is not responding until I close the process manually. then it outputs ''. same with .readline and .readlines. What that could be?
Halst
`read(10)` waits until it encounters EOF or has read 10 bytes. I have little experience with that, but you should use `select` or background threads to poll and read from the streams. There is also a warning about deadlocks in the `subprocess` documentation.
Philipp
what kind of "select" do you mean?
Halst
btw, p.stdin.write() works perfect. and all p.stdout.read*() works fine only after termination of the subprocess.
Halst