4
任何人都可以解释interrupt_main()方法在Python中的工作原理吗?Python主线程中断
我有了这个Python代码:
import time, thread
def f():
time.sleep(5)
thread.interrupt_main()
def g():
thread.start_new_thread(f,())
time.sleep(10)
print time.time()
try:
g()
except KeyboardInterrupt:
print time.time()
当我尝试运行它,它给了我下面的输出:
1380542215.5
# ... 10 seconds break...
1380542225.51
但是,如果我中断程序手动(CTRL-C),线程正确中断:
1380542357.58
^C1380542361.49
为什么线程中断只发生在10 se conds(而不是5)在第一个例子中?
我发现了一个ancient thread n Python mailing list,但它几乎没有解释。
一个KeyboardInterrupt意味着你是一个键盘中断。 – njzk2
两者均按预期工作。你怎么了? – falsetru
我期望主线程立即中断(5秒后)。 – Danstahr