What is the best way to repeatedly execute a function every x seconds?

后端 未结 18 3071
不知归路
不知归路 2020-11-21 06:04

I want to repeatedly execute a function in Python every 60 seconds forever (just like an NSTimer in Objective C). This code will run as a daemon and is effectively like call

18条回答
  •  耶瑟儿~
    2020-11-21 06:18

    Lock your time loop to the system clock like this:

    import time
    starttime = time.time()
    while True:
        print "tick"
        time.sleep(60.0 - ((time.time() - starttime) % 60.0))
    

提交回复
热议问题