哔哔one
您所链接的示例是错误的,当调用警报处理程序而不是读取块时,实际会出现异常。最好试试这个:import signal
TIMEOUT = 5 # number of seconds your want for timeoutdef interrupted(signum, frame):
"called when read times out"
print 'interrupted!'signal.signal(signal.SIGALRM, interrupted)def input():
try:
print 'You have 5 seconds to type in your stuff...'
foo = raw_input()
return foo except:
# timeout
return# set alarmsignal.alarm(TIMEOUT)s = input()# disable the alarm after successsignal.alarm(0)print 'You typed', s