python input timeout

Solutions on MaxInterview for python input timeout by the best coders in the world

showing results for - "python input timeout"
Ella
05 Sep 2019
1import signal
2TIMEOUT = 5 # number of seconds your want for timeout
3
4def interrupted(signum, frame):
5    "called when read times out"
6    print 'interrupted!'
7signal.signal(signal.SIGALRM, interrupted)
8
9def input():
10    try:
11            print 'You have 5 seconds to type in your stuff...'
12            foo = raw_input()
13            return foo
14    except:
15            # timeout
16            return
17
18# set alarm
19signal.alarm(TIMEOUT)
20s = input()
21# disable the alarm after success
22signal.alarm(0)
23print 'You typed', s
24