Home > Software engineering >  Python loop to run after n minutes from start time
Python loop to run after n minutes from start time

Time:02-28

I am trying to create a while loop which will iterate between 2 time objects, while datetime.datetime.now().time() <= datetime.datetime.now() relativedelta(hour=1): but on every n minutes or second interval. So if the starting time was 1:00 AM, the next iteration should being at 1:05 AM with n being 5 mins. So the iteration should begin after 5 mins and not for example, from the end of an iteration. Could you please advise how this could be accomplished?

CodePudding user response:

Did you try time.sleep(300) where 300 is seconds.

CodePudding user response:

if you want your program to run every 5 minuets you can use time.sleep

import time
while true:
    #program
    time.sleep(300)

if you want to iterate between dates use this template:

from datetime import timedelta
start_date = date_utils.parse('2021-01-01')
end_date = datetime.datetime.now()
while start_date <= end_date:
    one_hour = timedelta(hours=1)
    one_minute = timedelta(minutes=1)
    start_date = start_date   datetime.timedelta(days=1)
  • Related