interrupt_main的作用是:Raise a KeyboardInterrupt in the main thread.
下面的程序试图利用子线程中调用interrupt_main(),中断主线程中的raw_input(),用于限定10s内输入内容,结果10s后依然停留在输入状态,超时后输入回车后子线程中产生的中断才起作用,奇怪了!
from thread import interrupt_main,start_new_thread
from time import sleep
flag = 1
def timeout( secs ):
global flag
sleep(secs)
if(flag):
print "timeout"
interrupt_main( )
print "interrupt_main"
def lt_input():
global flag
flag = 1
start_new_thread(timeout,(10,))
try:
s = raw_input("input in 10s :")
flag = 0
except:
s = "no"
print s
lt_input()