I made this setInterval
kind of like in javascripts only for python and it's a little different, problem is I can't seem to figure out how to cancel it since when I cancel it, it just makes a new one and continues to run that one after I canceled the original thread
def setInterval(sec, func, *args, **kw):
def inner():
func(*args, **kw)
setInterval(sec, func, *args, **kw) # This is where it sends it again
task = threading.Timer(sec, inner)
task.daemon = True
task.start()
return task
As you can see, it works, however I have no way of canceling the thread because the original executes again and creates a new one before it can be canceled. How would I set this up so if the thread is canceled it won't create any copies of the same original thread? I've tried adding some keywords then sending the thread in it and have it cancel if it is a type of threading.Timer, but it doesn't seem to work since it already make a copy of the original before it could cancel, any ideas or suggestions? I'm just trying to think of a way so it knows that it's a copy of the original then not execute it but I'm not sure how I would actually do that. Is there anything I could do so it terminates/cancels the original thread and doesn't start a new copy of it before it has a chance to be canceled? Here is my attempt at doing it incase you want to tell me what I'm doing wrong.
def setInterval(sec = None, func = None, *args, **kw):
start = True if type(func) != threading.Timer else func.cancel() # doesn't work anyways
def inner():
func(*args, **kw)
setInterval(sec, func, *args, **kw)
if start == True:
task = thrading.Timer(sec, inner)
task.daemon = True
task.start()
return task
def clearInterval(task):
setInterval(func = task)
myInterval = setInterval(10, print, "Hello, world!")
clearInterval(myInterval) # cancels original, continues to make copies