trying to use popen2() to communicate with C program

I

I. Myself

I can't get this to work:

# commer.py - to test communication with other process
from popen2 import popen2
(child_stdout, child_stdin) = popen2("commer.exe")
print "Got here 1"
line = child_stdout.readline()
print "Got here 2"
child_stdin.write(line)
child_stdin.close
child_stdout.close

The compile C program, commer.exe, writes a line of text to its stdout.
The python program does not receive it; it never gets to print "Got here
2". Commer.exe does begin execution. Here's commer.c, but there
appears to be no problem with it. Commer.exe can be executed alone, and
it behaves as expected.

/* commer.c - to test communication with other process */
/* this program will output a line of text to stdout, and then
accept a line of input from stdin.
It also creates a text file called out.txt and writes to it.
*/
#include <stdio.h>
#include <stdlib.h>
int main() {
char line[] = "A line of text-----------------------------";
FILE *outfile;
/* Record that execution began: */
outfile = fopen("out.txt", "w");
fprintf(outfile, "commer.exe starting up.\n");
fclose(outfile);
/* Output a line of text to stdout: */
printf("%s\n", line);
/* Record that that happened: */
outfile = fopen("out.txt", "a");
fprintf(outfile, "text line was output to stdout\n");
fclose(outfile);
/* Get a line of text from stdin: */
scanf("%s", line);
/* Record the result: */
outfile = fopen("out.txt", "a");
fprintf(outfile,"%s was received.\n", line);
fclose(outfile);
return 0;
}

Thanks,

Mitchell Timin

--
I'm proud of http://ANNEvolve.sourceforge.net. I'm currently working on a
major update of the SailChallenge package. If you want to write software,
or articles, or do testing for ANNEvolve, let me know.

Humans may know that my email address is: (but remove the number)
zenguy at shaw789 dot ca.
 
D

Dennis Lee Bieber

The compile C program, commer.exe, writes a line of text to its stdout.
The python program does not receive it; it never gets to print "Got here
2". Commer.exe does begin execution. Here's commer.c, but there
appears to be no problem with it. Commer.exe can be executed alone, and
it behaves as expected.
Try flushing stdout... Many C runtimes detect when stdout is not a
console and go to a buffered output mode; without a flush, it may be
holding data until a (disk) block is filled...
--
 
I

I. Myself

Rene said:
I. Myself:


With what versions of what software on what platform?
I'm glad you asked. really!

Python 2.4 (#60, Nov 30 2004, 11:49:19) [MSC v.1310 32 bit (Intel)] on win32

Windows 2000. AMD processor. mingw compiler.

Mitchell Timin

--
I'm proud of http://ANNEvolve.sourceforge.net. I'm currently working on a
major update of the SailChallenge package. If you want to write software,
or articles, or do testing for ANNEvolve, let me know.

Humans may know that my email address is zenguy at shaw dot ca.
 
I

I. Myself

Dennis said:
Try flushing stdout... Many C runtimes detect when stdout is not a
console and go to a buffered output mode; without a flush, it may be
holding data until a (disk) block is filled...
That worked! I put fflush(stdout); after the printf() statement,
and that fixed it.

Thanks!

Mitchell Timin

I'm proud of http://ANNEvolve.sourceforge.net. I'm currently working on a
major update of the SailChallenge package. If you want to write software,
or articles, or do testing for ANNEvolve, let me know.

Humans may know that my email address is zenguy at shaw789 dot ca.
(Remove the 3-digit number.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top