今天从Gary Robinson的博客看到的,设置raw_input输入的超时时间,觉得挺实用的。代码如下:
#!/usr/bin/env python
import signal
class AlarmException(Exception):
pass
def alarmHandler(signum, frame):
raise AlarmException
def nonBockingRawInput(prompt='', timeout=20):
signal.signal(signal.SIGALRM, alarmHandler)
signal.alarm(timeout)
try:
text = raw_input(prompt)
signal.alarm(0)
return text
except AlarmException:
print 'Prompt timeout. Continuing...'
signal.signal(signal.SIGALRM, signal.SIG_IGN)
return ''
|
阅读(1619) | 评论(1) | 转发(0) |