如何設(shè)置RAW_INPUT的時間限制在python中,是否有一種方法在等待用戶輸入時計算時間,以便在30秒之后,raw_input()函數(shù)自動跳過?
3 回答

慕桂英3389331
TA貢獻2036條經(jīng)驗 獲得超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條經(jīng)驗 獲得超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
提交
取消