title: Getting output of a process at runtime
body: <p>I am using a python script to run a process using <code>subprocess.Popen</code> and simultaneously store the output in a text file as well as print it on the console. This is my code:</p>
  
  <pre><code>result = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
  for line in result.stdout.readlines(): #read and store result in log file
      openfile.write("%s\n" %line)
      print("%s" %line)
  </code></pre>
  
  <p>Above code works fine, but what it does is it first completes the process and stores the output in <em>result</em> variable. After that <em>for loop</em> stores the output as well as print it.</p>
  
  <p>But i want the output at runtime (as my process can take hours to complete, i don't get any output for all these hours).</p>
  
  <p>So is there any other function that gives me the output dynamically (at runtime), means as soon as the process gives first line, it should get printed.</p>