标题: interrupt_main好像没用
fuhuizn
新生入学
Rank: 1
注册用户



UID 24559
精华 0
积分 0(积分有什么用?)
帖子 15
阅读权限 10
注册 2002-9-16
状态 离线
发表于 2007-4-4 17:12  资料  个人空间  短消息  加为好友 
interrupt_main好像没用

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()


顶部
不容错过的IBM 集群技术资源汇总
 



当前时区 GMT+8, 现在时间是 2008-5-17 10:24

    Powered by Discuz! 5.5.0  © 2001-2007 Comsenz Inc.
Processed in 0.056747 second(s), 5 queries

清除 Cookies - 联系我们 - Linux伊甸园 - Archiver - WAP