-   Programming (
-   -   python: reading from stdin with a timeout (

eantoranz 07-09-2009 03:04 PM

python: reading from stdin with a timeout

I know it's possible to read from the standard input:


However, is it possible to set a timeout to the operation somehow?

Thanks in advance.

CSandman 07-09-2009 03:41 PM

You could fork a child that would do the read and then in the parent you could use sleep to wait for your timeout length and then just kill the child if it's still waiting. Off the top of my head I don't remember how the python code would look but it's pretty simple.

eantoranz 07-09-2009 03:43 PM

I'm trying with threading.Timer

eantoranz 07-09-2009 03:47 PM

Hell, it doesn't exit if sys.exit() is called from the function called from the Timer. Is that because of the call being in another thread? or because the readline() will refuse to quit?

eantoranz 07-09-2009 03:55 PM

Got it with a signal. Found this:

CSandman 07-09-2009 03:59 PM

Threading.Timer() is not what you want because it executes a command after some time. I was thinking you could fork a child and kill it after the timeout but that doesn't work because in order to get values back from the child you need to use a pipe() and, but is a blocking call...

let me ponder

CSandman 07-09-2009 04:00 PM

good find eantoranz I'll have to remember that trick!!

All times are GMT -5. The time now is 01:19 AM.