I can not catch the INT signal in the main thread, please tell me how to fix this problem. I want CTRL + C to interrupt the sleep method, but it waits until the timer expires.
import pygtk pygtk.require('2.0') import gtk import time import urllib2 import re import signal import sys import __main__ from time import ctime, strftime, localtime from threading import Thread myThread = None class MyThread(Thread): def __init__(self, filename): Thread.__init__(self) self.filename = filename; self.terminate = False def StopProcess(self): self.terminate = True def run(self): while self.terminate <> True: time.sleep(5) self.terminate = True def SignalHandler(signum, frame): if (myThread <> None): myThread.StopProcess() sys.exit() if __name__ == "__main__": signal.signal(signal.SIGINT, SignalHandler) myThread = MyThread("aaa") myThread.start()
Maymo source share