How can I capture output and show it at the same time with Python?
<p>I have a pretty long running job, which runs for several minutes and then gets restarted. The task outputs various information which I capture like this:</p>

<pre><code>output = subprocess.Popen(cmd,stdout=subprocess.PIPE).communicate()
</code></pre>

<p>The thing is, I will only get the entire output at a time. I would like to show output as the program is sending it to stdout, while still pushing it back in a buffer ( I need to check the output for the presence of some strings ). In Ruby I would do it like this:</p>

<pre><code>IO.popen(cmd) do |io|
  io.each_line do |line|
    puts line
    buffer &lt;&lt; line
  end
end
</code></pre>
