LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   python: reading from stdin with a timeout (http://www.linuxquestions.org/questions/programming-9/python-reading-from-stdin-with-a-timeout-738987/)

eantoranz 07-09-2009 04:04 PM

python: reading from stdin with a timeout
 
Hi!

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

Code:

line=sys.stdin.readline()
However, is it possible to set a timeout to the operation somehow?

Thanks in advance.

CSandman 07-09-2009 04: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 04:43 PM

I'm trying with threading.Timer

eantoranz 07-09-2009 04: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 04:55 PM

Got it with a signal. Found this:

http://www.velocityreviews.com/forum...nd-prompt.html

CSandman 07-09-2009 04: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 os.read(), but os.read() is a blocking call...

let me ponder

CSandman 07-09-2009 05:00 PM

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


All times are GMT -5. The time now is 06:34 PM.