Issue
I have been using the schedule module to try and schedule functions in my python code but I think my use case exceeded its capability.
At the moment I have 4 functions that I would like to run on a set schedule. I would like to run function_1 every hour, function_2 and function_3 on alternating hours, and function_4 once a week on its own.
My current job schedule is as follows:
schedule.every(1).hours.do(function_1)
schedule.every(2).hours.do(function_2)
schedule.every(3).hours.do(function_3)
schedule.every().monday.do(function_4_setup)
def function_4_setup()
logger.debug("Clearing Schedule to run function_4 on its own")
schedule.clear()
sleep(3600)
function_4()
logger.debug("Restarting All Jobs")
recreate_jobs() # re-adds function 1-3 to schedule
The problem is this code will result in function_2 and function_3 running at the same time or immediately after one another after 12 hours 18 hours and 24 hours which I would like to avoid.
To give you an example of the behavior I'm expecting:
- 00:00: function_1 & function_2 runs
- 01:00: function_1 & function_3 runs
- 02:00: function_1 & function_2 runs
- 03:00: function_1 & function_3 runs
... 1 week later...
- 00:00 function_4 runs
- 01:00 function_1 & function_2 runs
I have taken a look at APScheduler but that library also doesn't appear to accommodate my use case. Any advice/help would be greatly appreciated!
Solution
Instead of scheduling function 2 & 3 separately, I recommend making a function that flip flops the 2 functions and instead run that every hour. For example:
boolean = True
def flip_funcs():
global boolean
boolean = not boolean
[function_2, function_3][boolean]()
schedule.every(1).hours.do(flip_funcs)
Answered By - Teejay Bruno