如何设置RAW_INPUT的时间限制在python中,是否有一种方法在等待用户输入时计算时间,以便在30秒之后,raw_input()函数自动跳过?
3 回答
慕桂英3389331
TA贡献2036条经验 获得超8个赞
import signalclass AlarmException(Exception): passdef alarmHandler(signum, frame): raise AlarmExceptiondef 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 ''
慕的地6264312
TA贡献1817条经验 获得超6个赞
from threading import Timerdef input_with_timeout(x): def time_up(): answer= None print 'time up...'t = Timer(x,time_up) # x is amount of time in secondst.start()try: answer = input("enter answer : ")except Exception: print 'pass\n' answer = Noneif 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
添加回答
举报
0/150
提交
取消