title: User input with a timeout, in a loop
body: <p>I'm trying to create a looping python function which performs a task and prompts the user for a response and if the user does not respond in the given time the sequence will repeat.</p>
  
  <p>This is loosely based off this question: <a href="https://stackoverflow.com/questions/2933399/how-to-set-time-limit-on-input">How to set time limit on raw_input</a></p>
  
  <p>The task is represented by <code>some_function()</code>. The timeout is a variable in seconds. I have two problems with the following code:</p>
  
  <ol>
  <li><p>The raw_input prompt does not timeout after the specified time of 4 seconds regardless of whether the user prompts or not.</p></li>
  <li><p>When raw_input of 'q' is entered (without '' because I know anything typed is automatically entered as a string) the function does not exit the loop.</p></li>
  </ol>
  
  <p>`</p>
  
  <pre><code>import thread
  import threading
  from time import sleep
  
  def raw_input_with_timeout():
      prompt = "Hello is it me you're looking for?"
      timeout = 4
      astring = None
      some_function()
      timer = threading.Timer(timeout, thread.interrupt_main)
      try:
          timer.start()
          astring = raw_input(prompt)
      except KeyboardInterrupt:
          pass
      timer.cancel()
      if astring.lower() != 'q':
          raw_input_with_timeout()
      else:
          print "goodbye"
  </code></pre>
  
  <p>`</p>