I have followed the example which is shown here.
It is working as expected, but with a small problem. If the do_job() method takes more than a minute to finish. It's waiting for another 1 min from end of the do_job() method and scheduling another calculation.
But I want it to start timings 1 min from the start of execution time. Is there a possibility to do that?
EDIT
import schedule
import time
def job():
print("Process triggered at:", time.strftime("%Y-%m-%d %H:%M:%S"))
time.sleep(30)
print("Process ended at:", time.strftime("%Y-%m-%d %H:%M:%S"))
# Define the frequency (in seconds) at which the process should be triggered
frequency_seconds = 60 # Trigger every 60 seconds
# Schedule the job to run at the specified frequency
schedule.every(frequency_seconds).seconds.do(job)
# Main loop to keep the script running
while True:
schedule.run_pending()
time.sleep(1) # Sleep for 1 second to avoid high CPU usage
In the above code , the output generated is as below :
Process triggered at: 2024-03-25 06:57:23
Process ended at: 2024-03-25 06:57:53
Process triggered at: 2024-03-25 06:58:54
Process ended at: 2024-03-25 06:59:24
It is observed that the next process is triggered after the first process has ended. The time difference between first process end time and second process start time is 60 seconds.
But my expectation is the time difference between first process trigger time and second process trigger time should be 60 seconds. 60 seconds should not be calculation from process end time. job() should be called exactly at 60 seconds interval. So the expected output will look like this :
Process triggered at: 2024-03-25 06:57:23
Process ended at: 2024-03-25 06:57:53
Process triggered at: 2024-03-25 06:58:23
Process ended at: 2024-03-25 06:58:53
do_job()takes longer than a minute to run, you're not going to be able to successfully schedule it to run every minute. That aside, there's a library called schedule that might meet your needs.schedule.every().minute.at(":00").do(job)may work better. Also, the docs say "Schedule does not account for the time it takes for the job function to execute. To guarantee a stable execution schedule you need to move long-running jobs off the main-thread (where the scheduler runs). See Parallel execution for a sample implementation."