title: Grab user input asynchronously and pass to an Event loop in python
body: <p>I am building a single player MUD, which is basically a text-based combat game. It is not networked.</p>
  
  <p>I don't understand how to gather user commands and pass them into my event loop asynchronously. The player needs to be able to enter commands at any time as game events are firing. So pausing the process by using raw_input won't work. I think I need to do something like select.select and use threads.</p>
  
  <p>In the example below, I have a mockup function of userInputListener() which is where I like to receive commands, and append them to the command Que if there is input.</p>
  
  <p>If have an event loop such as:</p>
  
  <pre><code>from threading import Timer
  import time
  
  #Main game loop, runs and outputs continuously
  def gameLoop(tickrate):
  
      #Asynchronously get some user input and add it to a command que 
      commandQue.append(userInputListener())
      curCommand = commandQue(0)
      commandQue.pop(0)
  
      #Evaluate input of current command with regular expressions
      if re.match('move *', curCommand):
          movePlayer(curCommand)
      elif re.match('attack *', curCommand):
          attackMonster(curCommand)
      elif re.match('quit', curCommand):
          runGame.stop()
      #... etc    
  
      #Run various game functions...
      doStuff()
  
      #All Done with loop, sleep
      time.sleep(tickrate)
  
  #Thread that runs the game loop
  runGame = Timer(0.1, gameLoop(1))
  runGame.start()
  </code></pre>
  
  <p>How do I get my user input in there?</p>
  
  <p>Or more simply, can anyone show me any example of storing user input while another loop is running at the same time? I can figure out the rest if we can get that far.</p>