title: How to (can I) ask a PIPE how many bytes it has available for reading?
body: <p>I've implemented a non-blocking reader in Python, and I need to make it more efficient.</p>
  
  <p>The background: I have massive amounts of output that I need to read from one subprocess (started with Popen()) and pass to another thread. Reading the output from that subprocess must not block for more than a few ms (preferably for as little time as is necessary to read available bytes).</p>
  
  <p>Currently, I have a utility class which takes a file descriptor (stdout) and a timeout. I <code>select()</code> and <code>readline(1)</code> until one of three things happens:</p>
  
  <ol>
  <li>I read a newline</li>
  <li>my timeout (a few ms) expires</li>
  <li>select tells me there's nothing to read on that file descriptor.</li>
  </ol>
  
  <p>Then I return the buffered text to the calling method, which does stuff with it.</p>
  
  <p>Now, for the real question: because I'm reading so much output, I need to make this more efficient. I'd like to do that by asking the file descriptor how many bytes are pending and then <code>readline([that many bytes])</code>. It's supposed to just pass stuff through, so I don't actually care where the newlines are, or even if there are any. <strong>Can I ask the file descriptor how many bytes it has available for reading, and if so, how?</strong></p>
  
  <p>I've done some searching, but I'm having a really hard time figuring out what to search for, let alone if it's possible.</p>
  
  <p>Even just a point in the right direction would be helpful.</p>
  
  <p>Note: I'm developing on Linux, but that shouldn't matter for a "Pythonic" solution.</p>