Skip to content

Instantly share code, notes, and snippets.

@bigtan
Created February 2, 2016 03:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bigtan/93844d0862e4d5391275 to your computer and use it in GitHub Desktop.
Save bigtan/93844d0862e4d5391275 to your computer and use it in GitHub Desktop.
A Python RepeatTimer in standard library style.
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