tags:

views:

318

answers:

2

I'm trying to run wrap a simple (windows) command line tool up in a PyQt GUI app that I am writing. The problem I have is that the command line tool throws it's progress out to stdout (it's a server reset command so you get "Attempting to stop" and "Restarting" type output.

What I am trying to do is capture the output so I can display it as part of my app. I assumed it would be quite simple to do something like the following :

import os
import subprocess as sub
cmd = "COMMAND LINE APP NAME -ARGS"
proc = sub.Popen(cmd, shell=True, stdout=sub.PIPE).stdout
while 1:
    line = proc.readline()
    if not line: 
        break
print line

This partially works in that I do get the contents of StdOut but instead of as the progress messages are sent I get it once the command line application exits and it seems to flush StdOut in one go.

Is there a simple answer??

+1  A: 

Interactive communication through stdin/stdout is a common problem.

You're in luck though, with PyQt you can use QProcess, as described here: http://diotavelli.net/PyQtWiki/Capturing_Output_from_a_Process

wuub
A: 

Do I understand the question? I believe you're running something like "echo first; sleep 60; echo second" and you want see the "first" well-ahead of the "second", but they're both spitting out at the same time.

The reason you're having issues is that the operating system stores the output of processes in its memory. It will only take the trouble of sending the output to your program if the buffer has filled, or the other program has ended. So, we need to dig into the O/S and figure out how to tell it "Hey, gimme that!" This is generally known as asynchronous or non-blocking mode.

Luckily someone has done the hard work for us. This guy has added a send() and recv() method to the python built-in Popen class. It also looks like he fixed the bugs that people found in the comments.

Try it out: http://code.activestate.com/recipes/440554/

bukzor