title: Why is standard output from subprocess (redirected to unbuffered file) being buffered?
body: <p>From <a href="http://docs.python.org/library/functions.html#open" rel="noreferrer">http://docs.python.org/library/functions.html#open</a></p>
  
  <blockquote>
    <p>The optional bufsize argument
    specifies the file’s desired buffer
    size: 0 means unbuffered, 1 means line
    buffered, any other positive value
    means use a buffer of (approximately)
    that size. A negative bufsize means to
    use the system default, which is
    usually line buffered for tty devices
    and fully buffered for other files. If
    omitted, the system default is used.</p>
  </blockquote>
  
  <p>I'm passing 0 as bufsize below yet without using flush() there's no output written to the file when I run main_process.<br>
  What's the reason?</p>
  
  <pre><code># --------------------------------- sub_process.py
  import sys
  import time
  
  if __name__ == '__main__':
      print 'printed from redirect.py'
      # why is the following flush() needed? 'std-output' is (?) unbuffered...
      sys.stdout.flush() 
      time.sleep(6)
  
  
  # --------------------------------- main_process.py
  import subprocess
  import time
  
  if __name__ == '__main__':
      p = subprocess.Popen(
          ['python', 'sub_process.py'],
          stdout=open('std-output', 'w', 0))
      time.sleep(3)
      p.terminate()
  </code></pre>