我如何在龙卷风中的特定时间调用函数

How I can calling a function at specific time in tornado

如何在龙卷风中调用特定 data/time 处的函数? 我尝试使用 tornado 库中的函数 call_at,但没有按我预期的那样工作。

def call_at(self, when, callback, *args, **kwargs)

文档说有必要 subclass ioloop class 并覆盖函数,但我不明白如何正确地做,我认为这可能超出我的范围。

您应该使用 PeriodicCallback

classtornado.ioloop.PeriodicCallback(回调,callback_time,io_loop=None)

不,您不需要子类化任何东西。您只需调用 ioloop.IOLoop.current() 获取当前 运行ning ioloop 实例,然后调用 call_at 以 运行 您的函数。

但是,使用 call_latercall_at 更容易。

使用 call_later 的示例:

ioloop.IOLoop.current().call_later(delay=10, callback=your_function)

# Tornado will run `your_function` after 10 seconds.

如果您仍想使用 call_at,这里有一个例子:

current_time = ioloop.IOLoop.current().time()
call_time = current_time + 10
ioloop.IOLoop.current().call_at(when=call_time, callback=your_function)

# Tornado will run `your_function` after 10 seconds

更新:

在特定时间运行一个函数,你可以这样做:

from datetime import datetime

# take note of the current time
now = datetime.now()

# create a datetime object of when you want to call your function
call_time = datetime(year=2018, month=7, day=18, hour=14, minute=30) 

# find the time difference in seconds between `call_time` and `now`
call_time_seconds = (call_time - now).seconds

ioloop.IOLoop.current().call_later(delay=call_time_seconds, callback=your_function)
# Tornado will run your function at 14:30 on 18 July.