22

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?

abccd
  • 23,977
  • 10
  • 56
  • 68
calccrypto
  • 7,651
  • 19
  • 63
  • 96
  • related: [Python 3 Timed Input /15528939](https://stackoverflow.com/questions/15528939/python-3-timed-input) – n611x007 Mar 13 '15 at 13:14
  • related: [Keyboard input with timeout in Python /1335507](https://stackoverflow.com/questions/1335507/keyboard-input-with-timeout-in-python) – n611x007 Mar 13 '15 at 13:15
  • related: [raw_input and timeout /3471461](https://stackoverflow.com/questions/3471461/raw-input-and-timeout) – n611x007 Mar 13 '15 at 13:15

7 Answers7

31

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.:

import thread
import threading

def raw_input_with_timeout(prompt, timeout=30.0):
    print(prompt, end=' ')    
    timer = threading.Timer(timeout, thread.interrupt_main)
    astring = None
    try:
        timer.start()
        astring = input(prompt)
    except KeyboardInterrupt:
        pass
    timer.cancel()
    return astring

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:

import msvcrt
import time

def raw_input_with_timeout(prompt, timeout=30.0):
    print(prompt, end=' ')    
    finishat = time.time() + timeout
    result = []
    while True:
        if msvcrt.kbhit():
            result.append(msvcrt.getche())
            if result[-1] == '\r':   # or \n, whatever Win returns;-)
                return ''.join(result)
            time.sleep(0.1)          # just to yield to other processes/threads
        else:
            if time.time() > finishat:
                return None

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 my return 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.

martineau
  • 99,260
  • 22
  • 139
  • 249
Alex Martelli
  • 762,786
  • 156
  • 1,160
  • 1,345
  • hmm, I upvoted this, but now that I test it, it doesn't seem to work :s. You still have to press Enter (python 2.6.5 on Ubuntu Linux). – catchmeifyoutry May 29 '10 at 01:57
  • yeah. im testing your code right now, and i set it to 5 seconds, but like catchmeifyoutry said, you still have to wait until enter is pressed – calccrypto May 29 '10 at 02:06
  • there is also an interesting note in the python thread documentation: Caveat: Threads interact strangely with interrupts: the KeyboardInterrupt exception will be received by an arbitrary thread. (When the signal module is available, interrupts always go to the main thread.) – catchmeifyoutry May 29 '10 at 02:29
  • so any other idea on how to do this? i think Alex Martelli slightly misunderstood me: im trying to do something like those windows button warnings. after counting down, the input() automatically stops waiting and runs the rest of the program with a default value. i dont want a None answer after an hour of waiting. i dont want to wait past 30 seconds at all – calccrypto May 29 '10 at 02:35
  • 2
    @calccrypto, if you want a default different from `None`, add it as an argument to the function; I've now recoded it as Windows-only (but can't test it as I have no Windows) and done it so that it will terminate in 30 seconds, even if the user **is** slowly typing (rather than wait for 30 seconds of no typing, which seems a much more sensible interface to me) though I also mention how to easily get to a more sane behavior (you'd just need to reset the deadline after every typed character is successfully read, so only 30 seconds **of inaction** would result in the timeout behavior). – Alex Martelli May 29 '10 at 03:28
  • i want to just wait for 30 consecutive seconds of inaction. if nothing is typed, the default value is used. if something is typed but enter is not pressed within 30 seconds, the default value is used and the new code doesnt work on windows the default value doesnt really matter. just say the default value is 3.1415, and the input is waiting for that to be changed. and please excuse my silliness. i have never needed time and thread before – calccrypto May 29 '10 at 04:42
  • So the code I gave (second, Windows-specific one) should work, just changing `return None` to `return 3.1415`, no? – Alex Martelli May 29 '10 at 05:53
  • oh wait. that was a typo. you wrote kbkit, not kbhit. its working!!! thanks!!! – calccrypto May 29 '10 at 06:02
  • Ah yes, that's what comes of untested code, let me fix the typo. – Alex Martelli May 29 '10 at 06:39
  • wait.. where does the prompt string get used? – calccrypto May 29 '10 at 19:52
  • @calccrypto, clearly by a `print prompt,` that I had omitted -- I just edited the answer so it's there now (I'm not sure if you need a `sys.stdout.flush()` after it, and I don't have a Windows machine around to try;-). – Alex Martelli May 29 '10 at 21:34
  • I got a question: the second code will return whatever is entered in the console without pressing "Enter"!!! right? what I need is that the input should only be accepted when the key "Enter" is hit, otherwise, even if there is something on the console is not accepted and the message "Time up" should pop up with no need of pressing "Enter". I used the TRY/Except but the code stops right after!!! any suggestion?!!! – msc87 Feb 24 '14 at 13:16
  • 1
    The linux code doesn't work for the timeout doesn't work. – sidney May 22 '14 at 12:39
13

I found a solution to this problem in a blog post. Here's the code from that blog post:

import signal

class AlarmException(Exception):
    pass

def alarmHandler(signum, frame):
    raise AlarmException

def nonBlockingRawInput(prompt='', timeout=20):
    signal.signal(signal.SIGALRM, alarmHandler)
    signal.alarm(timeout)
    try:
        text = raw_input(prompt)
        signal.alarm(0)
        return text
    except AlarmException:
        print '\nPrompt timeout. Continuing...'
    signal.signal(signal.SIGALRM, signal.SIG_IGN)
    return ''

Please note: this code will only work on *nix OSs.

hichris123
  • 9,735
  • 15
  • 51
  • 66
jer
  • 19,474
  • 5
  • 43
  • 68
  • cool, but unfortunately, for some reason, the signal module doesnt have a "SIGALRM" attribute – calccrypto May 29 '10 at 01:36
  • 1
    @calccrypto, maybe you're on Windows? signal.SIGALRM is Unix-only (see my answer). – Alex Martelli May 29 '10 at 01:49
  • Right, sorry, should have noted it was Unix only. – jer May 29 '10 at 01:56
  • This seems to have all the same problems the other examples do, namely after the timeout expires, code execution does not continue. You have to hit enter. Has anyone solved this problem? – MydKnight Jun 18 '15 at 23:49
4
from threading import Timer


def input_with_timeout(x):    

def time_up():
    answer= None
    print('time up...')

t = Timer(x,time_up) # x is amount of time in seconds
t.start()
try:
    answer = input("enter answer : ")
except Exception:
    print('pass\n')
    answer = None

if answer != True:   # it means if variable have somthing 
    t.cancel()       # time_up will not execute(so, no skip)

input_with_timeout(5) # try this for five seconds

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!!

cinemassacres
  • 371
  • 3
  • 14
bilal hanif
  • 49
  • 1
  • 1
  • This needs user to enter "enter" .. i can't seem to get it to timeout. – Illusionist Jul 17 '13 at 18:09
  • I'm 3 years late, but: `raw_input` should be used rather than `input` (Python 2 is indicated by the `print`). In `time_up()` the read will not be cancelled unless `os._exit(1)` is called at its end. That could have other implications, but it is not easy to get rid of that console read. – cdarke Aug 26 '15 at 16:39
4

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 :

import tkinter as tk

def W_Input (label='Input dialog box', timeout=5000):
    w = tk.Tk()
    w.title(label)
    W_Input.data=''
    wFrame = tk.Frame(w, background="light yellow", padx=20, pady=20)
    wFrame.pack()
    wEntryBox = tk.Entry(wFrame, background="white", width=100)
    wEntryBox.focus_force()
    wEntryBox.pack()

    def fin():
        W_Input.data = str(wEntryBox.get())
        w.destroy()
    wSubmitButton = tk.Button(w, text='OK', command=fin, default='active')
    wSubmitButton.pack()

# --- optionnal extra code in order to have a stroke on "Return" equivalent to a mouse click on the OK button
    def fin_R(event):  fin()
    w.bind("<Return>", fin_R)
# --- END extra code --- 

    w.after(timeout, w.destroy) # This is the KEY INSTRUCTION that destroys the dialog box after the given timeout in millisecondsd
    w.mainloop()

W_Input() # can be called with 2 parameter, the window title (string), and the timeout duration in miliseconds

if W_Input.data : print('\nYou entered this : ', W_Input.data, end=2*'\n')

else : print('\nNothing was entered \n')
1

A curses example which takes for a timed math test

#!/usr/bin/env python3

import curses
import curses.ascii
import time

#stdscr = curses.initscr() - Using curses.wrapper instead
def main(stdscr):
    hd = 100 #Timeout in tenths of a second
    answer = ''

    stdscr.addstr('5+3=') #Your prompt text

    s = time.time() #Timing function to show that solution is working properly

    while True:
        #curses.echo(False)
        curses.halfdelay(hd)
        start = time.time()
        c = stdscr.getch()
        if c == curses.ascii.NL: #Enter Press
            break
        elif c == -1: #Return on timer complete
            break
        elif c == curses.ascii.DEL: #Backspace key for corrections. Could add additional hooks for cursor movement
            answer = answer[:-1]
            y, x = curses.getsyx()
            stdscr.delch(y, x-1)
        elif curses.ascii.isdigit(c): #Filter because I only wanted digits accepted
            answer += chr(c)
            stdscr.addstr(chr(c))
        hd -= int((time.time() - start) * 10) #Sets the new time on getch based on the time already used

    stdscr.addstr('\n')

    stdscr.addstr('Elapsed Time: %i\n'%(time.time() - s))
    stdscr.addstr('This is the answer: %s\n'%answer)
    #stdscr.refresh() ##implied with the call to getch
    stdscr.addstr('Press any key to exit...')
curses.wrapper(main)
dvrhax
  • 29
  • 4
0

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!

import time

def tastaturabfrage():

    inittime = int(time.time()) # time now
    waitingtime = 2.00          # time to wait in seconds

    while inittime+waitingtime>int(time.time()):

        key = win1.getch()      #check if keyboard entry or screen resize

        if key == curses.KEY_RESIZE:
            empty()
            resize()
            key=0
        if key == 118:
            p(4,'KEY V Pressed')
            yourfunction();
        if key == 107:
            p(4,'KEY K Pressed')
            yourfunction();
        if key == 99:
            p(4,'KEY c Pressed')
            yourfunction();
        if key == 120:
            p(4,'KEY x Pressed')
            yourfunction();

        else:
            yourfunction

        key=0
Daniel
  • 113
  • 9
Peter Fleix
  • 113
  • 9
0

This is for newer python versions, but I believe it will still answer the question. What this does is it creates a message to the user that the time is up, then ends the code. I'm sure there's a way to make it skip the input rather than completely end the code, but either way, this should at least help...

    import sys
    import time
    from threading import Thread
    import pyautogui as pag
    #imports the needed modules
    
    xyz = 1 #for a reference call

    choice1 = None #sets the starting status

    def check():
        time.sleep(15)#the time limit set on the message
        global xyz
        if choice1 != None:#if choice1 has input in it, than the time will not expire
            return
        if xyz == 1:#if no input has been made within the time limit, then this message will display
            pag.confirm(text = 'Time is up!', title = 'Time is up!!!!!!!!!')
            sys.exit()


    Thread(target = check).start()#starts the timer
    choice1 = input("Please Enter your choice: ")

I hope this helped :-)