Created
February 26, 2020 21:46
-
-
Save StiiCeva/bef507e1986c81fca876e46a3997f533 to your computer and use it in GitHub Desktop.
An example of how to repeteadly call a function in a thread
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 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