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

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

    e.g., Display current local time

    import datetime
    import glib
    import logger
    
    def get_local_time():
        current_time = datetime.datetime.now().strftime("%H:%M")
        logger.info("get_local_time(): %s",current_time)
        return str(current_time)
    
    def display_local_time():
        logger.info("Current time is: %s", get_local_time())
        return True
    
    # call every minute
    glib.timeout_add(60*1000, display_local_time)
    

提交回复
热议问题