-4

I have a for loop and I want each iteration to be executed every 5 minutes. Essentially, I want to freeze/sleep the loop for 5 minutes and then continue from where it left 5 minutes ago, NOT to start from the beginning. In total, I want to do this for an entire day (24hours).

  • 2
    Great! What have you tried so far? What issues are you running into? – esqew Oct 28 '19 at 16:02
  • Possible duplicate of [How do I get my Python program to sleep for 50 milliseconds?](https://stackoverflow.com/questions/377454/how-do-i-get-my-python-program-to-sleep-for-50-milliseconds) – JeffUK Oct 28 '19 at 16:03

2 Answers2

0

You could define a method that simply calls sleep for 5 minutes and then call that in your loop. Kind of like this

import time 

# main loop
for i in .....:
   # do your stuff
   wait5Mins()
   # continue doing stuff

def wait5Mins():
    time.sleep(300)
CEWeinhauer
  • 123
  • 1
  • 9
0

Something like this? The loop runs for (a little more than) twenty-four hours and sleeps for five minutes after each iteration.

from time import sleep, time

ONE_DAY = 24 * 60 * 60  # seconds
FIVE_MINUTES = 5 * 60  # seconds


start_time = time()

current_time = start_time
while current_time <= start_time + ONE_DAY - FIVE MINUTES:
    # Do something loopy here...

    time.sleep(FIVE_MINUTES)
    current_time = time()
Elias Strehle
  • 1,722
  • 1
  • 21
  • 34