I've read about this technique to timeout blocking IO operations, the problem is that it doesn't seem to work. for example:
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
this won't interrupt the main thread until raw_input()
returns.
Any help is appreciated.
Update:
Using os.kill(os.getpid(), signal.SIGINT)
instead of thread.interrupt_main()
seems to work (at least on Linux, which doesn't give me the portability I initially wanted). However, I'm still wondering why the code above doesn't work.