I cannot catch the INT signal in the main thread, please advise me how to fix the problem. I wish that the sleep method could be interrupted by CTRL+C, but it waits till the timer is ended..
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()
Believe it or not, this will work.
Alternatively, you can create some file descriptors using os.pipe() and then use select.select() on them within your signal_safe_sleep function. Both approaches will allow Python signal handlers to be called before signal_safe_sleep returns.