This question already has an answer here:
- Timeout on a function call 13 answers
in python, is there a way to, while waiting for a user input, count time so that after, say 30 seconds, the raw_input()
function is automatically skipped?
This question already has an answer here:
in python, is there a way to, while waiting for a user input, count time so that after, say 30 seconds, the raw_input()
function is automatically skipped?
The input() function is designed to wait for the user to enter something (at least the [Enter] key).
If you are not dead set to use input(), below is a much lighter solution using tkinter. In tkinter, dialog boxes (and any widget) can be destroyed after a given time.
Here is an example :
A curses example which takes for a timed math test
I found a solution to this problem in a blog post. Here's the code from that blog post:
Please note: this code will only work on *nix OSs.
under linux one could use curses and getch function, its non blocking. see getch()
https://docs.python.org/2/library/curses.html
function that waits for keyboard input for x seconds (you have to initialize a curses window (win1) first!
As it is self defined... run it in command line prompt , I hope you will get the answer read this python doc you will be crystal clear what just happened in this code!!
The signal.alarm function, on which @jer's recommended solution is based, is unfortunately Unix-only. If you need a cross-platform or Windows-specific solution, you can base it on threading.Timer instead, using thread.interrupt_main to send a
KeyboardInterrupt
to the main thread from the timer thread. I.e.:this will return None whether the 30 seconds time out or the user explicitly decides to hit control-C to give up on inputting anything, but it seems OK to treat the two cases in the same way (if you need to distinguish, you could use for the timer a function of your own that, before interrupting the main thread, records somewhere the fact that a timeout has happened, and in your handler for
KeyboardInterrupt
access that "somewhere" to discriminate which of the two cases occurred).Edit: I could have sworn this was working but I must have been wrong -- the code above omits the obviously-needed
timer.start()
, and even with it I can't make it work any more.select.select
would be the obvious other thing to try but it won't work on a "normal file" (including stdin) in Windows -- in Unix it works on all files, in Windows, only on sockets.So I don't know how to do a cross-platform "raw input with timeout". A windows-specific one can be constructed with a tight loop polling msvcrt.kbhit, performing a
msvcrt.getche
(and checking if it's a return to indicate the output's done, in which case it breaks out of the loop, otherwise accumulates and keeps waiting) and checking the time to time out if needed. I cannot test because I have no Windows machine (they're all Macs and Linux ones), but here the untested code I would suggest:The OP in a comment says he does not want to
return None
upon timeout, but what's the alternative? Raising an exception? Returning a different default value? Whatever alternative he wants he can clearly put it in place of myreturn None
;-).If you don't want to time out just because the user is typing slowly (as opposed to, not typing at all!-), you could recompute finishat after every successful character input.