Keyboard input with timeout?

Viewed 135493

How would you prompt the user for some input but timing out after N seconds?

Google is pointing to a mail thread about it at http://mail.python.org/pipermail/python-list/2006-January/533215.html but it seems not to work. The statement in which the timeout happens, no matter whether it is a sys.input.readline or timer.sleep(), I always get:

<type 'exceptions.TypeError'>: [raw_]input expected at most 1 arguments, got 2

which somehow the except fails to catch.

26 Answers

if you dont care how it works, just
pip install inputimeout
and

from inputimeout import inputimeout, TimeoutOccurred

if __name__ == "__main__":
    try:
        c = inputimeout(prompt='hello\n', timeout=3)
    except TimeoutOccurred:
        c = 'timeout'
    print(c)

so easy
https://pypi.org/project/inputimeout/

Here is a portable and simple Python 3 solution using threads. This is the only one that worked for me while being cross-platform.

Other things I tried all had problems:

  • Using signal.SIGALRM: not working on Windows
  • Using select call: not working on Windows
  • Using force-terminate of a process (instead of thread): stdin cannot be used in new process (stdin is auto-closed)
  • Redirection stdin to StringIO and writing directly to stdin: will still write to previous stdin if input() has already been called (see https://stackoverflow.com/a/15055639/9624704)
    from threading import Thread
    class myClass:
        _input = None

        def __init__(self):
            get_input_thread = Thread(target=self.get_input)
            get_input_thread.daemon = True  # Otherwise the thread won't be terminated when the main program terminates.
            get_input_thread.start()
            get_input_thread.join(timeout=20)

            if myClass._input is None:
                print("No input was given within 20 seconds")
            else:
                print("Input given was: {}".format(myClass._input))


        @classmethod
        def get_input(cls):
            cls._input = input("")
            return

my cross platform solution

def input_process(stdin_fd, sq, str):
    sys.stdin = os.fdopen(stdin_fd)
    try:
        inp = input (str)
        sq.put (True)
    except:
        sq.put (False)

def input_in_time (str, max_time_sec):
    sq = multiprocessing.Queue()
    p = multiprocessing.Process(target=input_process, args=( sys.stdin.fileno(), sq, str))
    p.start()
    t = time.time()
    inp = False
    while True:
        if not sq.empty():
            inp = sq.get()
            break
        if time.time() - t > max_time_sec:
            break
    p.terminate()
    sys.stdin = os.fdopen( sys.stdin.fileno() )
    return inp

For Linux, I would prefer the select version by @Pontus. Here just a python3 function works like read in shell:

import sys, select

def timeout_input(prompt, timeout=3, default=""):
    print(prompt, end=': ', flush=True)
    inputs, outputs, errors = select.select([sys.stdin], [], [], timeout)
    print()
    return (0, sys.stdin.readline().strip()) if inputs else (-1, default)

Run

In [29]: timeout_input("Continue? (Y/n)", 3, "y")                                                                                                                                                                  
Continue? (Y/n): 
Out[29]: (-1, 'y')

In [30]: timeout_input("Continue? (Y/n)", 3, "y")                                                                                                                                                                  
Continue? (Y/n): n

Out[30]: (0, 'n')

And a yes_or_no function

In [33]: yes_or_no_3 = lambda prompt: 'n' not in timeout_input(prompt + "? (Y/n)", 3, default="y")[1].lower()                                                                                                      

In [34]: yes_or_no_3("Continue")                                                                                                                                                                                   
Continue? (Y/n): 
Out[34]: True

In [35]: yes_or_no_3("Continue")                                                                                                                                                                                   
Continue? (Y/n): no

Out[35]: False
from threading import Thread
import time


def get_input():
    while True:
        print(input('> '))


t1 = Thread(target=get_input)
t1.setDaemon(True)
t1.start()
time.sleep(3)
print('program exceeds')

Well just simply set a new Daemon thread, and set a sleep time that whatever you want for timeout. I think that is easy to catch up XD

You can use in Python >= 3.4 the inputimeout lib. MIT License.

$ pip install inputimeout

from inputimeout import inputimeout, TimeoutOccurred
try:
    something = inputimeout(prompt='>>', timeout=5)
except TimeoutOccurred:
    something = 'something'
print(something)

It's been years already, but just incase someone bumps into this like I did recently trying to solve this sort of problem, there is an easy and faster way of achieving this using the func-timeout package. It has to be installed before use for most IDEs; you can install it via pip. The above link is self explanatory, but I will give an example on how I implemented it.

from func_timeout import FunctionTimedOut, func_timeout

try:
   ans = func_timeout(5, lambda: int(input('What is the sum of 2 and 3?\n')))
   print(ans)
except FunctionTimedOut:
   print(5)

func_timeout returns the value of the method in its argument, the question() function in this case. It also allows for other arguments that are needed for the function (see documentation). If the set time elapses (5 secs here) it raises a TimedOutException and runs the code in the except block.

Modified iperov answer that works for me (python3 win10 2019-12-09)

changes to iperov:

  • replace str with sstr as str is a function in python

  • add imports

  • add sleep to lower cpu usage of the while loop (?)

  • add if name=='main': #required by multiprocessing on windows

    import sys, os, multiprocessing, time

    def input_process(stdin_fd, sq, sstr):
        sys.stdin = os.fdopen(stdin_fd)
        try:
            inp = input(sstr)
            sq.put(True)
        except:
            sq.put(False)
    
    def input_in_time(sstr, max_time_sec):
        sq = multiprocessing.Queue()
        p = multiprocessing.Process(target=input_process, args=( sys.stdin.fileno(), sq, sstr))
        p.start()
        t = time.time()
        inp = False
        while True:
    
            if not sq.empty():
                inp = sq.get()
                break
            if time.time() - t > max_time_sec:
                break
    
            tleft=int( (t+max_time_sec)-time.time())
            if tleft<max_time_sec-1 and tleft>0:
                print('\n  ...time left '+str(tleft)+'s\ncommand:')
    
            time.sleep(2)
    
        p.terminate()
        sys.stdin = os.fdopen( sys.stdin.fileno() )
        return inp
    
    if __name__=='__main__':
        input_in_time("command:", 17)
    

This is the way I approached this problem. I haven't tested it thoroughly, and I'm not sure it doesn't have some important problems, but considering other solutions are far from perfect as well, I decided to share:

import sys
import subprocess


def switch():
    if len(sys.argv) == 1:
        main()
    elif sys.argv[1] == "inp":
        print(input(''))
    else:
        print("Wrong arguments:", sys.argv[1:])


def main():
    passw = input_timed('You have 10 seconds to enter password:', timeout=10)
    if passw is None:
        print("Time's out! You explode!")
    elif passw == "PasswordShmashword":
        print("H-h-how did you know you h-h-hacker")
    else:
        print("I spare your life because you at least tried")


def input_timed(*args, timeout, **kwargs):
    """
    Print a message and await user input - return None if timedout
    :param args: positional arguments passed to print()
    :param timeout: number of seconds to wait before returning None
    :param kwargs: keyword arguments passed to print()
    :return: user input or None if timed out
    """
    print(*args, **kwargs)
    try:
        out: bytes = subprocess.run(["python", sys.argv[0], "inp"], capture_output=True, timeout=timeout).stdout
    except subprocess.TimeoutExpired:
        return None
    return out.decode('utf8').splitlines()[0]


switch()

I am using a external tool inputimeout . Source code is available at github. I know it is a external tool but it is simple and quite handy. After installing the tool use this code:

from inputimeout import inputimeout, TimeoutOccurred
try:
    something = inputimeout(prompt='>>', timeout=5)
except TimeoutOccurred:
    something = 'No input.'
print(something)

Solution inspired by iperov's answer which is hopefully a bit cleaner:

import multiprocessing
import sys

def input_with_timeout(prompt, timeout=None):
    """Requests the user to enter a code at the command line."""
    queue = multiprocessing.Queue()
    process = multiprocessing.Process(
        _input_with_timeout_process, args=(sys.stdin.fileno(), queue, prompt),
    )
    process.start()
    try:
        process.join(timeout)
        if process.is_alive():
            raise ValueError("Timed out waiting for input.")
        return queue.get()
    finally:
        process.terminate()


def _input_with_timeout_process(stdin_file_descriptor, queue, prompt):
    sys.stdin = os.fdopen(stdin_file_descriptor)
    queue.put(input(prompt))

This is a Python 3.8+ (although it can be adapted to Python 3.6+) cross-platform approach that only uses threading (so no multiprocessing or calls to shell utilities). It is intended for running scripts from the command-line and isn't very suited for dynamical use.

You can wrap the builtin input function as follows. In this case I'm redefining the built-in name input as the wrapper, since this implementation requires all calls to input to be routed through this. (Disclaimer: that's why it's probably not a very good idea, just a different one, for fun.)

import atexit
import builtins
import queue
import threading


def _make_input_func():
    prompt_queue = queue.Queue(maxsize=1)
    input_queue = queue.Queue(maxsize=1)

    def get_input():
        while (prompt := prompt_queue.get()) != GeneratorExit:
            inp = builtins.input(prompt)
            input_queue.put(inp)
            prompt_queue.task_done()

    input_thread = threading.Thread(target=get_input, daemon=True)

    last_call_timed_out = False

    def input_func(prompt=None, timeout=None):
        """Mimics :function:`builtins.input`, with an optional timeout

        :param prompt: string to pass to builtins.input
        :param timeout: how long to wait for input in seconds; None means indefinitely

        :return: the received input if not timed out, otherwise None
        """
        nonlocal last_call_timed_out

        if not last_call_timed_out:
            prompt_queue.put(prompt, block=False)
        else:
            print(prompt, end='', flush=True)

        try:
            result = input_queue.get(timeout=timeout)
            last_call_timed_out = False
            return result
        except queue.Empty:
            print(flush=True) # optional: end prompt line if no input received
            last_call_timed_out = True
            return None


    input_thread.start()
    return input_func


input = _make_input_func()
del _make_input_func

(I've defined the setup in the one-use-only _make_input_func to hide input's "static" variables in its closure, in order to avoid polluting the global namespace.)

The idea here is to make a separate thread which handles any and all calls to builtins.input, and make the input wrapper manage the timeout. Since a call to builtins.input always blocks until there is input, when the timeout is over, the special thread is still waiting for input, but the input wrapper returns (with None). At the next call, if the last call timed out, it doesn't need to call builtins.input again (since the input thread has already been waiting for input), it just prints the prompt, and then waits for said thread to return some input, as always.

Having defined the above, try running the following script:

import time

if __name__ == '__main__':
    timeout = 2
    start_t = time.monotonic()
    if (inp := input(f"Enter something (you have {timeout} seconds): ", timeout)) is not None:
        print("Received some input:", repr(inp))
    else:
        end_t = time.monotonic()
        print(f"Timed out after {end_t - start_t} seconds")

    inp = input("Enter something else (I'll wait this time): ")
    print("Received some input:", repr(inp))
    
    input(f"Last chance to say something (you have {timeout} seconds): ", timeout)

Some of the answers require to press the Enter key when the timeout occurs to continue running your code. Others seem to be convoluted, and to boot, still require to press the Enter key after timeout.

I found the answer in another thread, which works beautifully, but there's a caveat that I found. I decided to place my code in a class for portability.

Note

I had to use keyboard to inject the Enter key press, since I had another input() statement in my code. For some reason, the subsequent input() statement wouldn't appear unless I pressed the Enter key.

import threading
import keyboard    # https://github.com/boppreh/keyboard

class Utilities:

    # Class variable
    response = None

    @classmethod
    def user_input(cls, timeout):

        def question():
            cls.response = input("Enter something: ")

        t = threading.Thread(target=question)
        # Daemon property allows the target function to terminate after timeout
        t.daemon = True    
        t.start()
        t.join(timeout)

        if cls.response:
            # Do something
        else:
            # Do something else
            # Optional.  Use if you have other input() statements in your code
            keyboard.send("enter")

Usage

Utilities.user_input(3)

This was made with Python 3.8.3 on Windows 10.

Here is one more that python 3.8+ on linux that includes a yes_no answer with default return on timeout

import signal
def alarm_handler(signum, frame):
    raise TimeoutError
def input_with_timeout(prompt, timeout=30):
    """ get input with timeout

    :param prompt: the prompt to print
    :param timeout: timeout in seconds, or None to disable

    :returns: the input
    :raises: TimeoutError if times out
    """
    # set signal handler
    if timeout is not None:
        signal.signal(signal.SIGALRM, alarm_handler)
        signal.alarm(timeout) # produce SIGALRM in `timeout` seconds
    try:
        return input(prompt)
    except TimeoutError as to:
        raise to
    finally:
        if timeout is not None:
            signal.alarm(0) # cancel alarm

def yes_or_no(question, default='y', timeout=None):
    """ Get y/n answer with default choice and optional timeout

    :param question: prompt
    :param default: the default choice, i.e. 'y' or 'n'
    :param timeout: the timeout in seconds, default is None

    :returns: True or False
    """
    if default is not None and (default!='y' and default!='n'):
        log.error(f'bad option for default: {default}')
        quit(1)
    y='Y' if default=='y' else 'y'
    n='N' if default=='n' else 'n'
    while "the answer is invalid":
        try:
            to_str='' if timeout is None else f'(Timeout {default} in {timeout}s)'
            reply = str(input_with_timeout(f'{question} {to_str} ({y}/{n}): ',timeout=timeout)).lower().strip()
        except TimeoutError:
            log.warning(f'timeout expired, returning default={default} answer')
            reply=''
        if len(reply)==0:
            return True if default=='y' else False
        elif reply[0] == 'y':
            return True
        if reply[0] == 'n':
            return False

Example of use in code


if yes_or_no(f'model {latest_model_folder} exists, start from it?', timeout=TIMEOUT):
     log.info(f'initializing model from {latest_model_folder}')
     model = load_model(latest_model_folder)
else:
     log.info('creating new empty model')
     model = create_model()

Extending the previous answer, which uses inputimeout, with a simple illustration

from inputimeout import inputimeout, TimeoutOccurred

def timed_input (user_prompt, timeout=5):
    user_input = ""
    timed_out = False
    try:
        user_input = inputimeout (prompt=user_prompt, timeout=timeout)
    except TimeoutOccurred:
        timed_out = True
    return (timed_out, user_input)

timed_out, user_input = timed_input ("Enter something within 3s... ", timeout=3)

if timed_out:
    print ("You failed to enter anything!")
else:
    print (f"You entered {user_input}")

This is the code I've written. Using multiprocessing, we can timeout input.

from multiprocessing import Queue, Process
from queue import Empty

class ProcessTimedOutException(Exception):
    def __init__(self, message: str):
        self.message: str = message


class Terminal:

    @staticmethod
    def input_with_timeout(message: str = '', timeout: int = 60) -> Tuple[Optional[str], Optional[Exception]]:
        queue = Queue()
        err: Optional[Exception] = None
        user_input: Optional[str] = None
        input_thread = Process(target=Terminal._input_async, args=(queue, message), daemon=True)
        input_thread.start()
        try:
            user_input = queue.get(timeout=timeout)
        except Empty:
            input_thread.terminate()
            err = ProcessTimedOutException(f'process timed out')
        return user_input, err

    @staticmethod
    def _input_async(queue, message: str = ''):
        sys.stdin = open(0)
        user_input = input(message).strip()
        queue.put(user_input)


if __name__ == '__main__':
    input_message: str = 'enter anything'
    user_input, err = Terminal.input_with_timeout(message=input_message,timeout=60)
    if err is not None:
        raise err
    print(user_input)                    

I had the same problem and solved it with keyboard and kthread. As soon as you press enter, the input field disappears. This was the most important thing for me, but I couldn't make it work with other approaches.

If you want, you can install it using pip:

pip install input-timeout

Here are some examples:

        from input_timeout import InputTimeout



        i = InputTimeout(

            timeout=20,

            input_message=" >> ",

            timeout_message="'Sorry, you were not fast enough'",

            defaultvalue="slow",

            cancelbutton="esc",

            show_special_characters_warning='If you want to use special characters, you have to use alt+\\d\\d\\d\\d\nPress "ctrl" to see a complete list of all combinations!',

        ).finalvalue

        print(f"\n\nYour input was {i}")



        i = InputTimeout(

            timeout=5,

            input_message=" >> ",

            timeout_message="Sorry, you were not fast enough: ",

            defaultvalue="slow",

            cancelbutton="esc",

            show_special_characters_warning='If you want to use special characters, you have to use alt+\\d\\d\\d\\d\nPress "ctrl" to see a complete list of all combinations!',

        ).finalvalue

        print(f"\n\nYour input was {i}")



        i = InputTimeout(

            timeout=10,

            input_message=" >> ",

            timeout_message="Sorry, you were not fast enough",

            defaultvalue="Wake up!",

            cancelbutton=None,

            show_special_characters_warning=None,

        ).finalvalue

        print(f"\n\nYour input was {i}")



        i = InputTimeout(

            timeout=10,

            input_message=" >> ",

            timeout_message="Sorry, you were not fast enough",

            defaultvalue="Are you sleeping?",

            cancelbutton="esc",

            show_special_characters_warning=None,

        ).finalvalue

        print(f"\n\nYour input was {i}")



        i = InputTimeout(

            timeout=10,

            input_message=" >>",

            timeout_message="Sorry, you were not fast enough",

            defaultvalue="you are so slow",

            cancelbutton=None,

            show_special_characters_warning='If you want to use special characters, you have to use alt+\\d\\d\\d\\d\nPress "ctrl" to see a complete list of all combinations!',

        ).finalvalue

        print(f"\n\nYour input was {i}")

#output

If you want to use special characters, you have to use alt+\d\d\d\d

Press "ctrl" to see a complete list of all combinations!

 >>  babba

Your input was babba

If you want to use special characters, you have to use alt+\d\d\d\d

Press "ctrl" to see a complete list of all combinations!

alt+0192    ->  À       alt+0193    ->  Á       alt+0196    ->  Ä       alt+0194    ->  Â       

alt+0195    ->  Ã       alt+0197    ->  Å       alt+0198    ->  Æ       alt+0228    ->  ä       

alt+0224    ->  à       alt+0225    ->  á       alt+0226    ->  â       alt+0227    ->  ã       

alt+0229    ->  å       alt+0230    ->  æ       alt+0199    ->  Ç       alt+0231    ->  ç       

alt+0208    ->  Ð       alt+0240    ->  ð       alt+0203    ->  Ë       alt+0200    ->  È       

alt+0201    ->  É       alt+0202    ->  Ê       alt+0235    ->  ë       alt+0232    ->  è       

alt+0233    ->  é       alt+0234    ->  ê       alt+0207    ->  Ï       alt+0204    ->  Ì       

alt+0205    ->  Í       alt+0206    ->  Î       alt+0239    ->  ï       alt+0236    ->  ì       

alt+0237    ->  í       alt+0238    ->  î       alt+0209    ->  Ñ       alt+0241    ->  ñ       

alt+0214    ->  Ö       alt+0210    ->  Ò       alt+0211    ->  Ó       alt+0212    ->  Ô       

alt+0213    ->  Õ       alt+0216    ->  Ø       alt+0140    ->  Œ       alt+0246    ->  ö       

alt+0242    ->  ò       alt+0243    ->  ó       alt+0244    ->  ô       alt+0245    ->  õ       

alt+0248    ->  ø       alt+0156    ->  œ       alt+0138    ->  Š       alt+0223    ->  ß       

alt+0154    ->  š       alt+0222    ->  Þ       alt+0254    ->  þ       alt+0220    ->  Ü       

alt+0217    ->  Ù       alt+0218    ->  Ú       alt+0219    ->  Û       alt+0252    ->  ü       

alt+0249    ->  ù       alt+0250    ->  ú       alt+0251    ->  û       alt+0159    ->  Ÿ       

alt+0221    ->  Ý       alt+0255    ->  ÿ       alt+0253    ->  ý       alt+0168    ->  ¨       

alt+0136    ->  ˆ       alt+0180    ->  ´       alt+0175    ->  ¯       alt+0184    ->  ¸       

alt+0192    ->  À       alt+0193    ->  Á       alt+0196    ->  Ä       alt+0194    ->  Â       

alt+0195    ->  Ã       alt+0197    ->  Å       alt+0198    ->  Æ       alt+0228    ->  ä       

alt+0224    ->  à       alt+0225    ->  á       alt+0226    ->  â       alt+0227    ->  ã       

alt+0229    ->  å       alt+0230    ->  æ       alt+0199    ->  Ç       alt+0231    ->  ç       

alt+0208    ->  Ð       alt+0240    ->  ð       alt+0203    ->  Ë       alt+0200    ->  È       

alt+0201    ->  É       alt+0202    ->  Ê       alt+0235    ->  ë       alt+0232    ->  è       

alt+0233    ->  é       alt+0234    ->  ê       alt+0207    ->  Ï       alt+0204    ->  Ì       

alt+0205    ->  Í       alt+0206    ->  Î       alt+0239    ->  ï       alt+0236    ->  ì       

alt+0237    ->  í       alt+0238    ->  î       alt+0209    ->  Ñ       alt+0241    ->  ñ       

alt+0214    ->  Ö       alt+0210    ->  Ò       alt+0211    ->  Ó       alt+0212    ->  Ô       

alt+0213    ->  Õ       alt+0216    ->  Ø       alt+0140    ->  Œ       alt+0246    ->  ö       

alt+0242    ->  ò       alt+0243    ->  ó       alt+0244    ->  ô       alt+0245    ->  õ       

alt+0248    ->  ø       alt+0156    ->  œ       alt+0138    ->  Š       alt+0223    ->  ß       

alt+0154    ->  š       alt+0222    ->  Þ       alt+0254    ->  þ       alt+0220    ->  Ü       

alt+0217    ->  Ù       alt+0218    ->  Ú       alt+0219    ->  Û       alt+0252    ->  ü       

alt+0249    ->  ù       alt+0250    ->  ú       alt+0251    ->  û       alt+0159    ->  Ÿ       

alt+0221    ->  Ý       alt+0255    ->  ÿ       alt+0253    ->  ý       alt+0168    ->  ¨       

alt+0136    ->  ˆ       alt+0180    ->  ´       alt+0175    ->  ¯       alt+0184    ->  ¸       

Sorry, you were not fast enough: 

Your input was slow

 >>  super

Your input was super

 >>  adasa

Your input was adasa

If you want to use special characters, you have to use alt+\d\d\d\d

Press "ctrl" to see a complete list of all combinations!

Sorry, you were not fast enough

Your input was you are so slow

A late answer :)

I would do something like this:

from time import sleep

print('Please provide input in 20 seconds! (Hit Ctrl-C to start)')
try:
    for i in range(0,20):
        sleep(1) # could use a backward counter to be preeety :)
    print('No input is given.')
except KeyboardInterrupt:
    raw_input('Input x:')
    print('You, you! You know something.')

I know this is not the same but many real life problem could be solved this way. (I usually need timeout for user input when I want something to continue running if the user not there at the moment.)

Hope this at least partially helps. (If anyone reads it anyway :) )

Related