免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 7876 | 回复: 8
打印 上一主题 下一主题

关于多线程不能接受中断信号 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-06-13 10:14 |只看该作者 |倒序浏览
我现在做了一个计时器类,如果在时间没达到之前,能够按CTRL-C进行信号中断

程序代码如下:

#!/usr/bin/python
#FileName:timer.py
import threading
import time
import signal



def stopSignal(signum,frame):
    global threadTimer
    print "it is getting stopsignal"
    threadTimer.stop()
   
signal.signal(signal.SIGINT,stopSignal)        


class Timer(threading.Thread):
    def __init__(self,threadName,interval,startTime,sharedObject):
        threading.Thread.__init__(self,name=threadName)
        self.__interval=interval
        self.sharedObject=sharedObject
        self.activeStatus=False
        self.__startTime=startTime
     
        

    def run(self):
        self.activeStatus=self.sharedObject.getActive()
         
        while self.activeStatus:
            now=time.localtime(time.time())
            compareTime=time.strftime("%H:%M",now)
            if compareTime==self.__startTime:
                print "The function is starting"
                self.sharedObject.setActive(False)
                self.activeStatus=self.sharedObject.getActive()

            else:
                print "The function is waiting"
                time.sleep(self.__interval)
               

           
               
            
        print "The thread is stopped"         

    def stop(self):
        self.sharedObject.setActive(False)
        self.activeStatus=self.sharedObject.getActive()
  
        
        
   
class SynchoronizedActive:

    def __init__(self):
        self.active=True
        self.threadCondition=threading.Condition()

    def setActive(self,boolean):
        self.threadCondition.acquire()
        self.active=boolean
        self.threadCondition.notify()
        self.threadCondition.release()

    def getActive(self):
        self.threadCondition.acquire()
        tempResult=self.active
        self.threadCondition.notify()
        self.threadCondition.release()
        return tempResult

   
           



        


theadActive=SynchoronizedActive()
threadTimer=Timer('threadTimer',20,'10:00',theadActive)
threadTimer.start()

threadTimer.join()
print "The program is end"


  如果中途按CTRL-C,程序不会停止,等到程序运行结束后
结果如下;
>>>
The function is waiting
The function is waiting
The function is waiting
The function is waiting
The function is waiting
The function is starting
The thread is stopped
it is getting stopsignal
The program is end

这说明程序只能等到线程结束后才接受到了中断信号。因为如果他在中途接受中断信号至少会打印
it is getting stopsignal
请高手帮忙看下。

[ 本帖最后由 lusec3 于 2006-6-13 10:16 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2006-06-13 10:47 |只看该作者
问题可能就是,在子线程中,在进入休眠后。主线程虽然接受到了中断信号。但不能更改休眠后的子线程的数据。

论坛徽章:
0
3 [报告]
发表于 2006-06-13 16:52 |只看该作者
怎么没人回答我
#!/usr/bin/python
#FileName:timer.py
import threading
import time
import signal





class Timer(threading.Thread):
    def __init__(self,threadName,interval,startTime,sharedObject):
        threading.Thread.__init__(self,name=threadName)
        self.__interval=interval
        self.sharedObject=sharedObject
  
        self.__startTime=startTime
     
        

    def run(self):
        getStatus=self.sharedObject.getActive()
         
        while getStatus:

            now=time.localtime(time.time())
            compareTime=time.strftime("%H:%M",now)
            if compareTime==self.__startTime:
                print "The function is starting\n"
                self.sharedObject.setActive(False)
                getStatus=self.sharedObject.getActive()

            else:
                print "The function is waiting\n"
                print self.getName(),"done sleeping\n"
                time.sleep(self.__interval)
                getStatus=self.sharedObject.getActive()
               
            
            
        print "The thread is stopped"         

    def stop(self):
        self.sharedObject.setActive(False)
        getStatus=self.sharedObject.getActive()
  
        
        
   
class SynchoronizedActive:

    def __init__(self):
        self.activeStatus=True
        self.threadCondition=threading.Condition()

    def setActive(self,boolean):
        self.threadCondition.acquire()
        self.activeStatus=boolean
        self.threadCondition.notify()
        self.threadCondition.release()

    def getActive(self):
        self.threadCondition.acquire()
        tempResult=self.activeStatus
        self.threadCondition.notify()
        self.threadCondition.release()
        return tempResult

   
           

class getCtrlThread(threading.Thread):

    def __init__(self,threadName,interval,sharedObject):
        threading.Thread.__init__(self,name=threadName)
        self.__interval=interval
        self.sharedObject=sharedObject
        signal.signal(signal.SIGINT,self.stopSignal)
        self.ctrlStop=True
    def run(self):
      
        while self.ctrlStop:
            print self.getName(),"done sleeping\n"
            time.sleep(self.__interval)
            
            
      

    def stopSignal(self,signum,frame):
        print "it is getting stopsignal\n"
        self.sharedObject.setActive(False)
        self.ctrlStop=False
      
        

        


theadActive=SynchoronizedActive()
threadTimer=Timer('threadTimer',4,'16:39',theadActive)
threadGetCtrl=getCtrlThread('threadgetCtrl',4,theadActive)
threadTimer.start()
threadGetCtrl.start()
print "return the mainthread\n"
time.sleep(4)
print "return the mainthread\n"
threadTimer.join()

threadGetCtrl.join()
print "The program is end"

这里如果我在程序运行开始的时候可以执行中断,过了几秒后在按ctrl-c就无效了。所有中断只能在
threadGetCtrl.join()语句执行之前发生。不然完全等待子线程运行。所有中断都失去效了。


请问斑竹怎样解决。

论坛徽章:
0
4 [报告]
发表于 2006-06-14 09:25 |只看该作者
怎么没有人回答我。

论坛徽章:
0
5 [报告]
发表于 2006-06-15 15:32 |只看该作者
设为后台线程吧.

论坛徽章:
0
6 [报告]
发表于 2007-12-18 14:36 |只看该作者
汗~ 搜到这个帖子翻出来,不知lz最终是否解决和如何解决。
可以考虑1个全局变量控制,
在接受键盘中断时改变此参数,然后在线程里面补捉

论坛徽章:
4
CU大牛徽章
日期:2013-03-13 15:29:07CU大牛徽章
日期:2013-03-13 15:29:49CU大牛徽章
日期:2013-03-13 15:30:192015亚冠之广州恒大
日期:2015-07-22 17:20:15
7 [报告]
发表于 2007-12-18 23:01 |只看该作者
楼上说的是 比喻一个全局变量
子线程的while里 每次循环都检查 这个标志
如果到达停止状态 break 线程run就执行完了 就退出了

论坛徽章:
0
8 [报告]
发表于 2007-12-19 23:47 |只看该作者
可以定义个属性或者设置全局变量,不过建议用twisted

论坛徽章:
0
9 [报告]
发表于 2007-12-21 15:02 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP