1

I have been writing this code and the time taken for the execution is about 3-4 seconds and I want it to run every 15 minutes I have been using this code

while True:
  if datetime.datetime.now().minute % 15 == 0:
  ...
  time.sleep(60)

now I cannot start the code to run exactly when the seconds on the clock are at 0 so I want the code to run exactly when the seconds are zero. Thanks in advance

Some kid
  • 2,417
  • 3
  • 14
  • 12
  • Also [Run function at a specific time in python](/q/50121539/), [How to run a python script at a specific time](/q/5677853/) – Brian61354270 Jun 06 '21 at 13:55
  • what you could do is ```now=datetime.datetime.now()```. Then you could do ```current_time = now.strftime("%S")``` to get the seconds. And then check ```if current_time=='00'``` and then ```time.sleep(900)``` for 15 minutes –  Jun 06 '21 at 13:56
  • There are quit a few other Q&A's if you search with `python start function every 15 minutes`. – wwii Jun 06 '21 at 14:06
  • Related: https://stackoverflow.com/q/57701095/480982 – Thomas Weller Jun 06 '21 at 14:42

1 Answers1

0

If you'll think about it - you're suspending your code for 60 seconds on time.sleep(60) but you're not considering the time it takes for the code to execute each time, which creates a time difference.

You could calculate the difference in time for the next execution to happen instead of constant 60

BTW, If you want to execute a code repeatedly you could use crontab to execute your scripts for you, it's simple and on-time so you won't need to worry about timings

Some kid
  • 2,417
  • 3
  • 14
  • 12