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).
Asked
Active
Viewed 2,116 times
-4
-
2Great! 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 Answers
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
-
Thank you very much for your response, I used the answer I received above first and it works. But thanks for your help anyway! – Vassilis Koutsopoulos Oct 29 '19 at 13:29
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
-
Thank you very much, I adjusted it to my code and it worked! – Vassilis Koutsopoulos Oct 29 '19 at 13:28