title: Streaming pipes in Python
body: <p>I'm trying to convert the output of vmstat into a CSV file using Python, so I use something like this to convert to CSV and add the date and time as coloumns:</p>
  
  <pre><code>vmstat 5 | python myscript.py &gt;&gt; vmstat.log
  </code></pre>
  
  <p>The problem I'm having is it blocks while trying to iterate sys.stdin. It seems like the input buffer doesn't get flushed. I don't want to endlessly loop around and burn processor time as I'm trying to measure this. Here's a simple demonstration which blocks on line 3:</p>
  
  <pre><code>import sys
  
  for line in sys.stdin:
      sys.stdout.write(line)
      sys.stdout.flush()
  </code></pre>
  
  <p>Is there an easy way to access the stream immediately like grep does, without pausing while the input buffer fills up?</p>