From cd162d08621b46bab1f23be07d48222eb1e97dbd Mon Sep 17 00:00:00 2001 From: slickpockets Date: Fri, 22 Nov 2024 09:06:24 +0800 Subject: [PATCH 1/2] added function to add time to next run of job --- docs/background-execution.rst | 69 ++++++++++++++++++----------------- schedule/__init__.py | 7 ++++ 2 files changed, 42 insertions(+), 34 deletions(-) diff --git a/docs/background-execution.rst b/docs/background-execution.rst index f185c8aa..c3672f77 100644 --- a/docs/background-execution.rst +++ b/docs/background-execution.rst @@ -7,50 +7,51 @@ This is an example of how you could do this: .. code-block:: python - import threading - import time +import threading +import time - import schedule +import schedule - def run_continuously(interval=1): - """Continuously run, while executing pending jobs at each - elapsed time interval. - @return cease_continuous_run: threading. Event which can - be set to cease continuous run. Please note that it is - *intended behavior that run_continuously() does not run - missed jobs*. For example, if you've registered a job that - should run every minute and you set a continuous run - interval of one hour then your job won't be run 60 times - at each interval but only once. - """ - cease_continuous_run = threading.Event() +def run_continuously(interval=1): + """Continuously run, while executing pending jobs at each + elapsed time interval. + @return cease_continuous_run: threading. Event which can + be set to cease continuous run. Please note that it is + *intended behavior that run_continuously() does not run + missed jobs*. For example, if you've registered a job that + should run every minute and you set a continuous run + interval of one hour then your job won't be run 60 times + at each interval but only once. + """ + cease_continuous_run = threading.Event() + class ScheduleThread(threading.Thread): + @classmethod + def run(cls): + while not cease_continuous_run.is_set(): + schedule.run_pending() + time.sleep(interval) + continuous_thread = ScheduleThread() + continuous_thread.start() + return cease_continuous_run - class ScheduleThread(threading.Thread): - @classmethod - def run(cls): - while not cease_continuous_run.is_set(): - schedule.run_pending() - time.sleep(interval) - continuous_thread = ScheduleThread() - continuous_thread.start() - return cease_continuous_run +def background_job(): + print('Hello from the background thread') - def background_job(): - print('Hello from the background thread') +schedule.every(30).seconds.do(background_job) +# Start the background thread +stop_run_continuously = run_continuously() +all_jobs = schedule.get_jobs() - schedule.every().second.do(background_job) +a = all_jobs[0] - # Start the background thread - stop_run_continuously = run_continuously() +# Do some other things... +time.sleep(10) - # Do some other things... - time.sleep(10) - - # Stop the background thread - stop_run_continuously.set() +# Stop the background thread +stop_run_continuously.set() diff --git a/schedule/__init__.py b/schedule/__init__.py index 8e12eeb7..ea0749de 100644 --- a/schedule/__init__.py +++ b/schedule/__init__.py @@ -663,6 +663,13 @@ def do(self, job_func: Callable, *args, **kwargs): self.scheduler.jobs.append(self) return self + def add_time_to_next_run(self, seconds=0, minutes=0, hours=0, days=0, weeks=0, months=0, years=0): + """ + function to add time to the next_run of a job and ONLY the next run + """ + time_in_seconds = seconds + (minutes * 60) + (hours * 3600) + (days * 86400) + (weeks * 604800) + (months * 259200) + (years * 31536000) + self.next_run = self.next_run + datetime.timedelta(seconds=time_in_seconds) + @property def should_run(self) -> bool: """ From 87cd5485cf893704216fdb21c2ee7c9d776a2f58 Mon Sep 17 00:00:00 2001 From: slickpockets Date: Fri, 22 Nov 2024 09:06:50 +0800 Subject: [PATCH 2/2] added function to add time to next run of job --- docs/background-execution.rst | 69 +++++++++++++++++------------------ 1 file changed, 34 insertions(+), 35 deletions(-) diff --git a/docs/background-execution.rst b/docs/background-execution.rst index c3672f77..f185c8aa 100644 --- a/docs/background-execution.rst +++ b/docs/background-execution.rst @@ -7,51 +7,50 @@ This is an example of how you could do this: .. code-block:: python -import threading -import time + import threading + import time -import schedule + import schedule -def run_continuously(interval=1): - """Continuously run, while executing pending jobs at each - elapsed time interval. - @return cease_continuous_run: threading. Event which can - be set to cease continuous run. Please note that it is - *intended behavior that run_continuously() does not run - missed jobs*. For example, if you've registered a job that - should run every minute and you set a continuous run - interval of one hour then your job won't be run 60 times - at each interval but only once. - """ - cease_continuous_run = threading.Event() - class ScheduleThread(threading.Thread): - @classmethod - def run(cls): - while not cease_continuous_run.is_set(): - schedule.run_pending() - time.sleep(interval) - continuous_thread = ScheduleThread() - continuous_thread.start() - return cease_continuous_run + def run_continuously(interval=1): + """Continuously run, while executing pending jobs at each + elapsed time interval. + @return cease_continuous_run: threading. Event which can + be set to cease continuous run. Please note that it is + *intended behavior that run_continuously() does not run + missed jobs*. For example, if you've registered a job that + should run every minute and you set a continuous run + interval of one hour then your job won't be run 60 times + at each interval but only once. + """ + cease_continuous_run = threading.Event() + class ScheduleThread(threading.Thread): + @classmethod + def run(cls): + while not cease_continuous_run.is_set(): + schedule.run_pending() + time.sleep(interval) -def background_job(): - print('Hello from the background thread') + continuous_thread = ScheduleThread() + continuous_thread.start() + return cease_continuous_run -schedule.every(30).seconds.do(background_job) + def background_job(): + print('Hello from the background thread') -# Start the background thread -stop_run_continuously = run_continuously() -all_jobs = schedule.get_jobs() -a = all_jobs[0] + schedule.every().second.do(background_job) -# Do some other things... -time.sleep(10) + # Start the background thread + stop_run_continuously = run_continuously() -# Stop the background thread -stop_run_continuously.set() + # Do some other things... + time.sleep(10) + + # Stop the background thread + stop_run_continuously.set()