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

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

    The easier way I believe to be:

    import time
    
    def executeSomething():
        #code here
        time.sleep(60)
    
    while True:
        executeSomething()
    

    This way your code is executed, then it waits 60 seconds then it executes again, waits, execute, etc... No need to complicate things :D

提交回复
热议问题