0

I want that loop start at 00 min and 2 sec or 05 min and 2 sec or 10 min and 2 sec, etc...

I have code which work but i feel so stupid because i think this can be solved in a better way. not the gopnik style.

How can I do it?

def time_now():
    now = datetime.now()
    current_time = now.strftime("%H:%M:%S")
    print("Current Time =", current_time)


while True:
         
        now = datetime.now()
        while ( now.minute == 0 and now.second == 2) \
           or ( now.minute == 5 and now.second == 2) \
               or ( now.minute == 10 and now.second == 2) \
                   or ( now.minute == 15 and now.second == 2) \
                       or ( now.minute == 20 and now.second == 2) \
                           or ( now.minute == 25 and now.second == 2) \
                               or ( now.minute == 35 and now.second == 2) \
                                   or ( now.minute == 40 and now.second == 2) \
                                       or ( now.minute == 45 and now.second == 2) \
                                           or ( now.minute == 50 and now.second == 2) \
                                               or ( now.minute == 55 and now.second == 2):
            
            time_now()
            #print("Current Time =", current_time)
            print('Something')
            time.sleep(300)
        


  • 1
    Use crontab: https://pypi.org/project/python-crontab/ – rdas Nov 18 '21 at 14:02
  • 1
    all your values `minute` are divided by 5 so check only once `now.minutes % 5 == 0`. Or use `now.minutes in (0,5,10,...55)`. If you use `crontab` then you can use `*/5` to run when minute is divided by 5 – furas Nov 18 '21 at 14:03
  • @rdas problem is that `crontab` (and `python-crontab`) can't check seconds. But code could sleep 2 seconds. – furas Nov 18 '21 at 14:10
  • tnx rdas! it work like charm. the simplest solutions are the best. – Wild_rasta Nov 18 '21 at 14:19

3 Answers3

3

You can use the modulus operator as all your time instances are divisible by 5:

while ( now.minute % 5 == 0 and now.second == 2):
    print('Something')
    time.sleep(300)
Haukland
  • 677
  • 8
  • 25
1

Why the second while loop? Do you want to freeze the machine* for that second - then why apply the time.sleep? Looks kinda superfluous.

*note machine in this instance = cpu core, not the interpreter. I'm also not sure if it will sleep you for 300 seconds or 300.999 seconds if that distinction is important.

Anyway, the mod approach will work fine given your params here, but is rather fixed to params always being compatible with modulus. Could try

while True:
     
    now = datetime.now()
    if ( now.minute in [0,5,10,15,20,25,30,35,40,45,50,55] and now.second == 2):
        #Do something
        pass
Amiga500
  • 1,258
  • 1
  • 6
  • 11
  • Might as well use a tuple for the container: no need to modify it later (clearer on intention and saves a little ram). Or even a set, since duplicate entries in a container used for membership tests make no sense, but I don't use sets as often as I should. @Wild_rasta this is the correct *general* solution to avoiding chained literal comparisons: this pattern is used a *lot* in python. – 2e0byo Dec 10 '21 at 13:46
0

You can use cronjobs for this type of aproach. You can read and apply from here

Edited: For managing seconds, you can simply use sleep function in the cronjob

Alexandru DuDu
  • 998
  • 1
  • 7
  • 19