title: Check if subprocess is still running while reading its output line by line
body: <p>I am starting a <code>subprocess</code> in Python and trying to read each line of output. Unfortunately I can't find a good way of testing if my processes is still alive. The standard method seems to be checking <code>poll()</code>, but that seems to always return <code>None</code>. Here is my code.</p>
  
  <pre><code>proc = Popen(sys.argv[1:], stdin=PIPE, stdout=PIPE, stderr=STDOUT)
  
  while True:
      process_line(proc.stdout.readline().decode('utf-8'))
  
      if not proc.poll():
          break
  
  for line in proc.communicate()[0].splitlines():
      process_line(line.decode('utf-8'))
  </code></pre>
  
  <p>I've also tried using <code>os.kill(proc.pid, 0)</code>, which works for non-spawned processes, but it seems that Python keeps a handle on processes it starts so <code>os.kill(proc.pid, 0)</code> always returns.</p>
  
  <p>What am I doing wrong?</p>