-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsignal.py
More file actions
35 lines (30 loc) · 772 Bytes
/
signal.py
File metadata and controls
35 lines (30 loc) · 772 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/usr/bin/python
import signal
import sys, select
#ValueError: signal only works in main thread
def interrupted(signum, frame):
#"called when read times out"
#print 'interrupted!'
signal.signal(signal.SIGALRM, 'no commands')
def cmdInput():
try:
#print 'You have 5 seconds to type in your stuff...'
foo = raw_input("Type 'q' to end: ")
return foo
except:
# timeout
return ''
# set catcher for no command
signal.signal(signal.SIGALRM, interrupted)
# set alarm
signal.alarm(1)
cmd = self.cmdInput()
# disable the alarm after success
signal.alarm(0)
#import sys, select
print "You have ten seconds to answer!"
i, o, e = select.select( [sys.stdin], [], [], 10 )
if (i):
print "You said", sys.stdin.readline().strip()
else:
print "You said nothing!"