Read from stdouton Popen on WinXP?

M

mhenry1384

I am trying to run a program and filter the output on Windows XP.
Since I want to filter the output, I'd like to read it a line at a time
and only print the lines I care about.

p = subprocess.Popen(["doxygen.exe", r"Doxyfile.cfg"],
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
while 1:
line = p.stdout.readline()
if not line:
break
print line


The problem is that readline() only returns after the whole process has
completed. I have tried various permutations such as using os.read()
and changing the bufsize parameter and using popen4. To no avail.

Obviously, it should be possible to read stdout before the process
completes, since if I leave off the "stdout=" parameter, the full
output shows up in stdout in "realtime" as you'd expect.

About the only thing I can come up with is to pipe the .exe to another
python script which could communicate to the main script via TCP/IP,
but that seems ridiculous. I searched the newsgroup and didn't see
anything particularly helpful.

Anyone have a non-ridiculous solution?
 
F

fraca7

mhenry1384 a écrit :
I am trying to run a program and filter the output on Windows XP.
Since I want to filter the output, I'd like to read it a line at a time
and only print the lines I care about.

I had the exact same problem yesterday. This is an obvious use case for
the "bufsize" parameter to os.popen4, except that the Windows
implementation doesn't accept anything but -1...

The only way I can see out of this is to use win32api to launch the
process through CreateProcess, then perform asynchronous I/O on its
stdout using the OVERLAPPED structure in a call to ReadFile... Ugly...
 

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,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top