异步函数的周期性回调

periodiccallback of asynchronous function

PeriodicCallback是否确保没有并行执行?回调是在前一个触发的仍然是 运行 时触发的,还是确保一次只有一个回调 运行?据我所知,后一个是真的,但我想确定一下!我应该控制异步函数的时间不超过下一个periodiccallback调度器。

例如:

如果我每 5 秒有一个 periodiccallback 并且我的函数时间有时(因为发出 http 请求等)更长(例如 7 秒),我怎样才能跳到“10 秒”并传递给15?只有当它发生时。

龙卷风文档说

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

因此,如果我对您的问题的理解正确,PeriodicCallback 已经完全满足您的要求如果您的回调是同步函数

如果您的回调不是同步的,那么您将需要使用锁来检查自己是否已经 运行。类似于:

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