Get realtime output from python subprocess
Question: I'm trying to invoke a command line utility from Python. The code is as
follows

    
    
    import subprocess
    import sys
    
    class Executor :
    
        def executeEXE(self,executable ) :
    
            CREATE_NO_WINDOW = 0x08000000
            process = subprocess.Popen(executable, stdout=subprocess.PIPE,
                                       creationflags=CREATE_NO_WINDOW )
            while True:
                line = process.stdout.readline()
                if line == '' and process.poll() != None:
                    break
                print line
    

The problem with above code is I want real time output of above process which
I'm not getting. What I'm doing wrong here.

I've referred 10's of articles but none seems to help.

Thanks in advance.

Regards, Ganesh.

P. S. Command line utility I'm invoking is Star Wind V2V (if some how that
matters)


Answer: there are 2 problems in your code:

  1. first of all, readline() will block untill when a new line is printed out and flushed.  
That means you should execute the code

while True:  
...

in a new Thread and call a callback function when the output is ready.

  2. Since the readline is waiting for a new line, you must use 

print 'Hello World'  
sys.stdout.flush()

everytime in your executable.

You can see some code and example on my git:  
[pyCommunicator](https://github.com/jstar88/pyCommunicator)

Instead, if your external tool is buffered, the only thing you can try is to
use stderr as PIPE: <http://stackoverflow.com/a/11902799/2054758>

