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. Its 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. Any advices are greatly appreciated.
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
I hope the desired output clarifies what I'm trying to achieve. Again thanks in advance for taking time to read and respond.
If your objective is to start jobs a given number of seconds since the start of the previous job rather than since the end of the previous job, you can use the following code:
If your concern is that new jobs cannot be started while previous jobs are still running. i.e. that you want to start new jobs every minute even if they take 90 seconds to run, you may have to use threads. Please say if that is the issue.