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

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

    I faced a similar problem some time back. May be http://cronus.readthedocs.org might help?

    For v0.2, the following snippet works

    import cronus.beat as beat
    
    beat.set_rate(2) # 2 Hz
    while beat.true():
        # do some time consuming work here
        beat.sleep() # total loop duration would be 0.5 sec
    

提交回复
热议问题