Skip to content

Instantly share code, notes, and snippets.

@StiiCeva
Created February 26, 2020 21:46
Show Gist options
  • Save StiiCeva/bef507e1986c81fca876e46a3997f533 to your computer and use it in GitHub Desktop.
Save StiiCeva/bef507e1986c81fca876e46a3997f533 to your computer and use it in GitHub Desktop.
An example of how to repeteadly call a function in a thread
class Job(threading.Thread):
def __init__(self, interval, execute, *args, **kwargs):
'''
Start class with .start()
:param interval: timedelta
:param execute: a function
:param args: list of args
:param kwargs: dict of args
'''
threading.Thread.__init__(self)
self.daemon = False
self.stopped = threading.Event()
self.interval = interval
self.execute = execute
self.args = args
self.kwargs = kwargs
def stop(self):
self.stopped.set()
self.join()
def run(self):
while not self.stopped.wait(self.interval.total_seconds()):
self.execute(*self.args, **self.kwargs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment