title: python - How select.select() works?
body: <h2>Background:</h2>
  <p>I'm familiar with C's <code>select()</code> function. I've been using this function for many purposes. Most of them, if not all, for reading and writing to pipes, files, etc. I must say that I've never used the error list, but this is not involved in the key question.</p>
  <h2>Question:</h2>
  <p>Does Python's <code>select()</code> behave as follows?</p>
  <p>It turns out to me that <code>select()</code> on Python behaves a different way despite the <a href="http://docs.python.org/library/select.html#module-select" rel="nofollow noreferrer"><em>straightforward</em></a> interface to C <code>select()</code>. It seems that <code>select()</code> returns the very first time a file is ready for reading. If you read the file while leaving some bytes to be read, calling <code>select()</code> will block. But, if you call <code>select()</code> again after a previous call to <code>select()</code> was returned without any read call between these two calls, <code>select()</code> will return as expected. For example:</p>
  <pre><code>import select
  # Open the file (yes, playing around with joysticks)
  file = open('/dev/input/js0', 'r') 
  # Hold on the select() function waiting
  select.select([file], [], [])
  # Say 16 bytes are sent to the file, select() will return.
  ([&lt;open file '/dev/input/js0', mode 'r' at 0x7ff2949c96f0&gt;], [], [])
  # Call select() again, and select() will indeed return.
  select.select([file], [], [])
  ([&lt;open file '/dev/input/js0', mode 'r' at 0x7ff2949c96f0&gt;], [], [])
  # read 8 bytes. There are 8 bytes left for sure. Calling again file.read(8) will empty the queue and would be pointless for this example
  file.read(8)
  '&lt;\t\x06\x01\x00\x00\x81\x01'
  # call select() again, and select() will block
  select.select([file], [], [])
  # Should it block? there are 8 bytes on the file to be read.
  </code></pre>
  <p>If this is the behaviour of <code>select()</code> in Python, I'm okay with that, I could handle it. Not what I expected, though, but it's fine. I know what I can do with it.</p>
  <p>But if this is not the behaviour of <code>select()</code> I would appreciate someone to tell me what I'm doing wrong. What I read about <code>select()</code> is what the Python docs say: &quot;select() returns if any file in the read|write|error list is ready for read|write|error.&quot;. That's OK no lies there. Maybe the questions should be:</p>
  <ul>
  <li>When a file is considered to be ready for reading in python?</li>
  <li>Does it means a file that has never been read?</li>
  <li>Does it means a file with bytes to be read?</li>
  </ul>