title: Python subprocess readlines() hangs
body: <p>The task I try to accomplish is to stream a ruby file and print out the output. (<strong><em>NOTE</em></strong>: I don't want to print out everything at once)</p>
  
  <p><strong>main.py</strong></p>
  
  <pre><code>from subprocess import Popen, PIPE, STDOUT
  
  import pty
  import os
  
  file_path = '/Users/luciano/Desktop/ruby_sleep.rb'
  
  command = ' '.join(["ruby", file_path])
  
  master, slave = pty.openpty()
  proc = Popen(command, bufsize=0, shell=True, stdout=slave, stderr=slave, close_fds=True)     
  stdout = os.fdopen(master, 'r', 0)
  
  while proc.poll() is None:
      data = stdout.readline()
      if data != "":
          print(data)
      else:
          break
  
  print("This is never reached!")
  </code></pre>
  
  <p><strong>ruby_sleep.rb</strong></p>
  
  <pre><code>puts "hello"
  
  sleep 2
  
  puts "goodbye!"
  </code></pre>
  
  <p><em><strong>Problem</em></strong></p>
  
  <p>Streaming the file works fine. The hello/goodbye output is printed with the 2 seconds delay. Exactly as the script should work. The problem is that readline() hangs in the end and never quits. I never reach the last print.</p>
  
  <p>I know there is a lot of questions like this here a stackoverflow but non of them made me solve the problem. I'm not that into the whole subprocess thing so please give me a more hands-on/concrete answer.</p>
  
  <p>Regards</p>
  
  <p><strong>edit</strong></p>
  
  <p>Fix unintended code. (nothing to do with the actual error)</p>