views:

109

answers:

1

This question is very similar to this one. I want to read output from a console app of mine. The app does not terminate, nor does it take input from stdin.

When I modify rix0rrr's solution to execute my app and then run his solution, Python hangs because read(1) does not return. The initial output of the app is "Starting the server.\n". Can you guess what property my app may have that is preventing his solution from working? The extent of my changes is that I changed this:

p = Popen( ["cmd.exe"], stdin=PIPE, stdout=PIPE )
prompt = re.compile(r"^C:\\.*>", re.M)

to this:

p = Popen( ["c:\\path\\to\\my\\app\\app.exe"], stdin=PIPE, stdout=PIPE )
prompt = re.compile(r"Starting", re.M)
import pdb;pdb.set_trace()

I also created a test version of my app that returns immediately and verified that the output from the app is returned by read() in that case. His original, unmodified example, as expected, also does not hang.

I also tried out the ActiveState code that Piotr linked to in his answer. No output is returned from the process in that case, either.

This is Python 2.4.4 on Vista.

+1  A: 

The very first thing I would check is the buffering in app.exe. If "Starting the server.\n" is being buffered and doesn't make it to the pipe, there is nothing you can do on the reader's side.

So, try adding fflush(stdout) after printf("Starting the server.\n").

Paul Du Bois
That was it! ..
Brian