python多线程不能执行中断信号

lusec3 2006-06-13 05:02:04
我现在做了一个计时器类,如果在时间没达到之前,能够按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
请高手帮忙看下。
...全文
587 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiangding 2006-06-21
  • 打赏
  • 举报
回复
呵呵,学习
michael_g_hu 2006-06-15
  • 打赏
  • 举报
回复
这个问题不是用QQ解决了吗?嘿嘿,给分
xyzxyz1111 2006-06-14
  • 打赏
  • 举报
回复
代码太长了, 贴个短点的可否?
lusec3 2006-06-14
  • 打赏
  • 举报
回复
怎么 没有人回答我
lusec3 2006-06-13
  • 打赏
  • 举报
回复
后来更改了下程序如下。但还是有问题



#!/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()语句执行之前发生。不然完全等待子线程运行。所有中断都失去效了。


请问斑竹怎样解决。

37,742

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • WuKongSecurity@BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧