title: Why does python keep buffering stdout even when flushing and using -u?
body: <pre><code>$ cat script.py
  import sys
  
  for line in sys.stdin:
      sys.stdout.write(line)
      sys.stdout.flush()
  
  $ cat script.py - | python -u script.py
  </code></pre>
  
  <p>The output is right but it only starts printing once I hit Ctrl-D whereas the following starts printing right away :</p>
  
  <pre><code>$ cat script.py - | cat
  </code></pre>
  
  <p>which led me to think that the buffering does not come from cat.</p>
  
  <p>I managed to get it working by doing :</p>
  
  <pre><code>for line in iter(sys.stdin.readline, ""):
  </code></pre>
  
  <p>as explained here : <a href="https://stackoverflow.com/questions/4187785/streaming-pipes-in-python">Streaming pipes in Python</a>, but I don't understand why the former solution doesn't work as expected.</p>