Created
February 2, 2016 03:00
-
-
Save bigtan/93844d0862e4d5391275 to your computer and use it in GitHub Desktop.
A Python RepeatTimer in standard library style.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RepeatTimer(Thread): | |
def __init__(self, interval, function, *args, **kwargs): | |
Thread.__init__(self) | |
self.interval = interval | |
self.function = function | |
self.args = args | |
self.kwargs = kwargs | |
self.finished = Event() | |
def cancel(self): | |
self.finished.set() | |
def run(self): | |
while not self.finished.is_set(): | |
self.finished.wait(self.interval) | |
if not self.finished.is_set(): | |
self.function(*self.args, **self.kwargs) | |
self.finished.set() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment