title: Keyboard input between select() in Python
body: <p>I write some codes to get the input from keyboard and also check something is alive or not:</p>
  
  <pre><code>import sys
  from select import select
  
  timeout = 10
  while is_alive(): # is_alive is a method to check some stuffs, might take 5 secs
      rlist, _, _ = select([sys.stdin], [], [], timeout)
      if rlist:
          s = sys.stdin.readline()
          print repr(s)
          handle(s) # handle is a method to handle and react according to input s
  </code></pre>
  
  <p>I found that when the keyboard input ends outside of the waiting in <code>select()</code> (usually it ends during the 5 secs of <code>is_alive()</code>), the <code>if rlist:</code> will get false.</p>
  
  <p>I can understand why but I don't know how to solve it.</p>
  
  <p>And there is still another question related to the situation mentioned above, sometimes <code>readline()</code> will return the last line of my input when some inputs are located across different <code>select()</code> waiting.</p>
  
  <p>That means, if I enter <strong>'abc\n'</strong> and unfortunately the <strong>'\n'</strong> located outside of wating in <code>select()</code> (that means, when I press <strong><em>Enter</em></strong>, the program are executing other parts, such as <code>is_alive()</code>), and then if I enter <strong>'def\n'</strong> and this time the <strong><em>Enter</em></strong> pressed successfully located within <code>select()</code>, I'll see the <code>s</code> from <code>readline()</code> becomes <strong>'def\n'</strong> and the first line is disappeared.</p>
  
  <p>Is there any good solution to solve two issues above? I'm using FreeBSD 9.0.</p>