From e29bf2baf655886b39af8032a3fecfb0f4e9d4cb Mon Sep 17 00:00:00 2001 From: Max Date: Wed, 27 Apr 2022 22:38:00 +0200 Subject: [PATCH] Added support for initial offset --- README.md | 14 ++++++++------ timeloop/app.py | 15 +++++++++++---- timeloop/job.py | 9 +++++++-- 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 81acc8e..eccccc6 100644 --- a/README.md +++ b/README.md @@ -5,10 +5,11 @@ Each job runs on a separate thread and when the service is shut down, it waits t Inspired by this blog [`here`](https://www.g-loaded.eu/2016/11/24/how-to-terminate-running-python-threads-using-signals/) +## Fork +This fork aims to provide some improvements to the original library. Mainly to be able to set a start time or start offset for tasks. + ## Installation -```sh -pip install timeloop -``` +Has to be installed manually atm. Since it is just a fork of the original and I still have to set that up and want to respect the work of Sankalp Jonna. Feel free to help tho! ## Writing jobs ```python @@ -28,9 +29,10 @@ def sample_job_every_5s(): print "5s job current time : {}".format(time.ctime()) -@tl.job(interval=timedelta(seconds=10)) -def sample_job_every_10s(): - print "10s job current time : {}".format(time.ctime()) +# Added support of initial offset! +@tl.job(interval=timedelta(seconds=10), offset=timedelta(hours=1)) +def sample_job_after_an_hour_every_10s(): + print "after an hour 10s job current time : {}".format(time.ctime()) ``` ## Start time loop in separate thread diff --git a/timeloop/app.py b/timeloop/app.py index d6808d1..f0644dd 100644 --- a/timeloop/app.py +++ b/timeloop/app.py @@ -1,3 +1,4 @@ +from datetime import datetime, timedelta import logging import sys import signal @@ -20,8 +21,8 @@ class Timeloop(): logger.setLevel(logging.INFO) self.logger = logger - def _add_job(self, func, interval, *args, **kwargs): - j = Job(interval, func, *args, **kwargs) + def _add_job(self, func, interval: timedelta, offset: timedelta=None, *args, **kwargs): + j = Job(interval, func, offset=offset *args, **kwargs) self.jobs.append(j) def _block_main_thread(self): @@ -46,9 +47,15 @@ class Timeloop(): self.logger.info("Stopping job {}".format(j.execute)) j.stop() - def job(self, interval): + def job(self, interval: timedelta, offset: timedelta=None): + """Decorator to define a timeloop for the decorated function. + + Args: + interval (timedelta): How long to wait after every execution until the next one. + offset (timedelta, optional): Positive offset until the first execution of the function. If None, will wait with first execution until the first interval passed. If timedelta with length 0 (or smaller) will execute immediately. Defaults to None. + """ def decorator(f): - self._add_job(f, interval) + self._add_job(f, interval, offset=offset) return f return decorator diff --git a/timeloop/job.py b/timeloop/job.py index df97d2f..0c0681c 100644 --- a/timeloop/job.py +++ b/timeloop/job.py @@ -1,12 +1,14 @@ from threading import Thread, Event from datetime import timedelta +from time import sleep class Job(Thread): - def __init__(self, interval, execute, *args, **kwargs): + def __init__(self, interval: timedelta, execute, offset: timedelta=None, *args, **kwargs): Thread.__init__(self) self.stopped = Event() - self.interval = interval + self.interval: timedelta = interval self.execute = execute + self.offset: timedelta = offset self.args = args self.kwargs = kwargs @@ -15,5 +17,8 @@ class Job(Thread): self.join() def run(self): + if self.offset: + sleep(self.offset.total_seconds()) + while not self.stopped.wait(self.interval.total_seconds()): self.execute(*self.args, **self.kwargs)