'How to stop this running threading.Thread?

I found this non blocking code on stack overflow which is using threads to provide functionality of nonblocking setInterval function in JavaScript. But when I try to stop the process it doesn't even the Ctrl + C is not stopping it, I have tried some more methods too stop the process but they are not working. Can someone please tell a right way to stop the process, thank you in advance.

here is the code

import threading

class ThreadJob(threading.Thread):
    def __init__(self,callback,event,interval):
        '''runs the callback function after interval seconds

        :param callback:  callback function to invoke
        :param event: external event for controlling the update operation
        :param interval: time in seconds after which are required to fire the callback
        :type callback: function
        :type interval: int
        '''
        self.callback = callback
        self.event = event
        self.interval = interval
        super(ThreadJob,self).__init__()

    def run(self):
        while not self.event.wait(self.interval):
            self.callback()



event = threading.Event()

def foo():
    print ("hello")

def boo():
    print ("fello")



def run():
    try:
        k = ThreadJob(foo,event,2)
        d = ThreadJob(boo,event,6)
        k.start()
        d.start()
        while 1:
            falg = input("Press q to quit")
            if(falg == 'q'):
                quit()
                return 
    except KeyboardInterrupt:
        print('Stoping the script...')         
    except Exception as e:
        print(e)

run()
print( "It is non-blocking")


Solution 1:[1]

All you need to do is to replace this line:

quit()

with this one:

event.set()

A Python program won't exit if there are threads still running, so the quit() function wasn't doing anything. The threads will exit their while loops once the event's internal flag is set, so a call to event.set() will cause the termination of both extra threads you created. Then the program will exit.

Note: technically you could set the threads to be "daemon" and then they will not keep the program alive. But that's not the right solution here, I think.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Paul Cornelius