Skip to content

Instantly share code, notes, and snippets.

@sebastianknopf
Created June 18, 2025 08:28
Show Gist options
  • Save sebastianknopf/0fd05dcbba293b35663c3e987fa27329 to your computer and use it in GitHub Desktop.
Save sebastianknopf/0fd05dcbba293b35663c3e987fa27329 to your computer and use it in GitHub Desktop.
repeatedtimer.py
from threading import Timer
class RepeatedTimer(object):
def __init__(self, interval, function, *args, **kwargs):
self._timer = None
self.interval = interval
self.function = function
self.args = args
self.kwargs = kwargs
self.is_running = False
def _run(self):
self.is_running = False
self.start()
self.function(*self.args, **self.kwargs)
def start_immediately(self):
self._run()
def start(self):
if not self.is_running:
self._timer = Timer(self.interval, self._run)
self._timer.start()
self.is_running = True
def stop(self):
self._timer.cancel()
self.is_running = False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment