Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically interrupting raw_input

Is there a way to programmatically interrupt Python's raw_input? Specifically, I would like to present a prompt to the user, but also listen on a socket descriptor (using select, for instance) and interrupt the prompt, output something, and redisplay the prompt if data comes in on the socket.

The reason for using raw_input rather than simply doing select on sys.stdin is that I would like to use the readline module to provide line editing functionality for the prompt.

like image 882
Ray Avatar asked Jul 02 '10 17:07

Ray


1 Answers

As far as I know... "Sort of".

raw_input is blocking so the only way I can think of is spawning a subprocess/thread to retrieve the input, and then simply communicate with the thread/subprocess. It's a pretty dirty hack (at least it seems that way to me), but it should work cross platform. The other alternative, of course, is to use either the curses module on linux or get this one for windows.

like image 196
Wayne Werner Avatar answered Nov 06 '22 01:11

Wayne Werner