pythonasynchronousparallel-processingtornadoperiodicity

periodiccallback of asynchronous function


Does PeriodicCallback ensure no parallel executions? Is the callback triggered while a previous triggered one is still running or does it ensure that there is only one callback running at a time? As far as I can see the later one is true, but I want to be sure! I should control that time of asynchronous function is no longer than next periodiccallback scheduler.

For example:

If I have a periodiccallback every 5 seconds and my function time sometimes (because makes http request etc) is longer (for example 7 second), how can I jump the "10 second" and pass to 15? Only when it happens.


Solution

  • The tornado docs says

    If the callback runs for longer than callback_time milliseconds, subsequent invocations will be skipped to get back on schedule.

    So if I'm understanding your question correctly, PeriodicCallback already does exactly what you want if your callback is a synchronous function!

    If your callback isn't synchronous, then you will need to use locks to check yourself if something is already running or not. Something like:

    from tornado import gen, locks
    
    lock = locks.Lock()
    running = False
    @gen.coroutine
    def task():
        global running
        with (yield lock.acquire()):
            if running:
                return
            running = True
        try:
            yield do_something()
        finally:
            with (yield lock.acquire()):
                running = False
    

    http://www.tornadoweb.org/en/stable/ioloop.html#tornado.ioloop.PeriodicCallback