带有Python超时的键盘输入您将如何提示用户输入,但在N秒后超时?Google正在指向一条关于它的邮件帖子http:/mail.python.org/pipermail/python-list/2006-1月/533215.html但这似乎行不通。超时发生的语句,无论是sys.input.readline还是timer.space(),我总是得到:<type 'exceptions.TypeError'>: [raw_]input expected at most 1 arguments, got 2不知道怎么就抓不到了。
3 回答
哔哔one
TA贡献1854条经验 获得超8个赞
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
添加回答
举报
0/150
提交
取消