Search code examples
pythontornado

Is there any way to change callback timeout in tornado PeriodicCallback instance?


I know about:

periodical_cllback_instance.stop() periodical_cllback_instance.start()

but it restarts with current callback_time ... is there any monkey patch to change callback_time after triggering stop() ... or some other ways?


Solution

  • You can just change the callback_time directly, you don't even need to stop the PeriodicCallback instance. This snipped seems to work for me:

    import tornado
    from tornado.ioloop import PeriodicCallback
    
    pc = None
    counter = 0
    
    
    def get_periodic_callback():
        global pc
        if pc is None:
            pc = PeriodicCallback(callback, 1000)
        return pc
    
    
    def callback():
        global counter
        if counter < 5:
            counter += 1
            print("foo")
        else:
            pc = get_periodic_callback()
            pc.callback_time = 100
            print("bar")
    
    
    pc = get_periodic_callback()
    pc.start()
    tornado.ioloop.IOLoop.instance().start()