title: How to implement a python REPL that nicely handles asynchronous output?
body: <p>I have a Python-based app that can accept a few commands in a simple read-eval-print-loop. I'm using <code>raw_input('&gt; ')</code> to get the input. On Unix-based systems, I also <code>import readline</code> to make things behave a little better. All this is working fine.</p>
  
  <p>The problem is that there are asynchronous events coming in, and I'd like to print output as soon as they happen. Unfortunately, this makes things look ugly. The "> " string doesn't show up again after the output, and if the user is halfway through typing something, it chops their text in half. It should probably redraw the user's text-in-progress after printing something.</p>
  
  <p>This seems like it must be a solved problem. What's the proper way to do this?</p>
  
  <p>Also note that some of my users are Windows-based.</p>
  
  <p>TIA</p>
  
  <p>Edit: The accepted answer works under Unixy platforms (when the readline module is available), but if anyone knows how to make this work under Windows, it would be much appreciated!</p>