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

后端 未结 18 2695
不知归路
不知归路 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:34

    One possible answer:

    import time
    t=time.time()
    
    while True:
        if time.time()-t>10:
            #run your task here
            t=time.time()
    

提交回复
热议问题