title: Read from pty without endless hanging
body: <p>I have a script, that prints colored output if it is on tty. A bunch of them executes in parallel, so I can't put their stdout to tty. I don't have control over the script code either (to force coloring), so I want to fake it via pty. My code:</p>
  
  <pre><code>invocation = get_invocation()
  master, slave = pty.openpty()
  subprocess.call(invocation, stdout=slave)
  print string_from_fd(master)
  </code></pre>
  
  <p>And I can't figure out, what should be in <code>string_from_fd</code>. For now, I have something like </p>
  
  <pre><code>def string_from_fd(fd):
    return os.read(fd, 1000)
  </code></pre>
  
  <p>It works, but that number <code>1000</code> looks strange . I think output can be quiet large, and any number there could be not sufficient. I tried a lot of solutions from stack overflow, but none of them works (it prints nothing or hanging forever).</p>
  
  <p>I am not very familiar with file descriptors and all that, so any clarification if I'm doing something wrong would be much appreciated.</p>
  
  <p>Thanks!</p>