I have a function that I need to run everyday from 5pm to 9am the next day, then starts again at 5pm. Below is my implementation:
import datetime as dt
import time
while True:
if dt.datetime.now().hour < 9 or dt.datetime.now().hour > 17:
#some irrelevant function here
print('Function processing')
time.sleep(1)
else:
print("Not time yet")
time.sleep(1)
continue
The above code works, but I would like to also execute a function once every first loop at 5pm and after the last loop at 9am. My first instinct is to do something like this:
def starting(): print("It's 9pm, time to start!")
def ending(): print("It's 5am, time to end")
while True:
if dt.datetime.now().hour < 9 or dt.datetime.now().hour > 17:
if #this is the first loop since 5pm:
starting()
elif #this is the last loop at 9am:
ending()
#some irrelevant function here
print('Function processing')
time.sleep(1)
else:
print("Not time yet")
time.sleep(1)
continue
I am not sure if this is the most optimal way of doing it, would appreciate some ideas.
Aucun commentaire:
Enregistrer un commentaire