【发布时间】:2012-06-05 00:36:22
【问题描述】:
我已经阅读了这种技术来超时阻塞 IO 操作,问题是它似乎不起作用。例如:
import thread, threading
def read_timeout(prompt, timeout=10.0):
timer = threading.Timer(timeout, thread.interrupt_main)
s = ''
timer.start()
try:
s = raw_input(prompt)
except KeyboardInterrupt:
print 'operation timed out.'
timer.cancel()
return s
s = read_timeout('enter input: ')
if s:
print 'you entered: %s' % s
在raw_input() 返回之前,这不会中断主线程。
任何帮助表示赞赏。
更新:
使用os.kill(os.getpid(), signal.SIGINT) 而不是thread.interrupt_main() 似乎可行(至少在Linux 上,这并没有给我最初想要的可移植性)。但是,我仍然想知道为什么上面的代码不起作用。
【问题讨论】:
标签: python multithreading timeout