Time as stopping condition of infinite loop

后端 未结 5 1787
我在风中等你
我在风中等你 2021-01-29 02:11
# run infinitly
while(True):

  done = False;

  while(not done):
    #Do Stuff
    #Main Program

    #stopping condition of inner while loop
    if datetime.datetime.n         


        
5条回答
  •  囚心锁ツ
    2021-01-29 02:39

    Rather than thrashing around constantly checking if it's 10 past the top of the hour, just do some math and sleep appropriately. Time is generally deterministic.

    import datetime
    import time
    
    def seconds_till_10past():
        now = datetime.datetime.now()
        delta = now.replace(minute=10, second=0) - now
        return delta.seconds % 3600
    
    while True:
        time.sleep(seconds_till_10past())
        print "Do something at 10 past the hour."
    

    Also, don't parenthesize arguments to statements (e.g. while(True)), it's bad form.

提交回复
热议问题