python-Can we use tempfile with subprocess to get non buffering live output in python app
<p>I am trying to run one python file from python windows application.For that I have used <code>subprocess</code>.For getting live streaming output on app console I have tried the below statements.</p>

<p>With PIPE</p>

<pre><code>p = subprocess.Popen(cmd,
                     stdout=subprocess.PIPE,
                     stderr=subprocess.STDOUT, shell=True)

for line in iter(p.stdout.readline, ''):
    print line
</code></pre>

<p>(or)</p>

<pre><code>process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
while True:
    out = process.stdout.read(1)
    if out == '' and process.poll() != None:
        break
    if out != '':
        sys.stdout.write(out)
        sys.stdout.flush()
</code></pre>

<p>Not only above code ,tried so many methods.Getting same results like below:</p>

<p><strong>1.Python windows app taking so much of time to run</strong></p>

<p><strong>2.Then the app window went to "not responding" state for long time</strong></p>

<p><strong>3.Then whole output is printed on the console</strong></p>

<p>I know that the buffer overflow is happening in python app thats why i am not getting live output.</p>

<p>I posted so many queries for this still not getting solution.</p>

<p>Just now found and tried tempfile for this.But i am not sure this will give live streaming output.</p>

<p>Shall I try this way?</p>

<pre><code>import tempfile
import subprocess

w = tempfile.NamedTemporaryFile()
p = subprocess.Popen(cmd, shell=True, stdout=w, 
                        stderr=subprocess.STDOUT, bufsize=0)

with open(w.name, 'r') as r:
    for line in r:
        print line
w.close()
</code></pre>

<p>Or any other best solutions for non blocking,unbuffering live output on windows app.</p>

<p>Any help would be appreciated.</p>

<p><strong>Note</strong>:1.The python file which I want to run has more print statements(ie more content)</p>

<p>2.Windows server 2012,python 2.7</p>