TypeError: Unsupported deadline datetime.datetime(....)
TypeError: Unsupported deadline datetime.datetime(....)
我正在尝试 运行 在 Tornado 中定期使用 IOLoop 的函数。但是,下面的代码会报错?
TypeError: Unsupported deadline datetime.datetime(2021, 11, 11, 0, 35, 49, 445200)
from tornado.ioloop import IOLoop
import datetime
def schedule_next_email():
# t = datetime.date.today() + datetime.timedelta(seconds=10)
t = datetime.datetime.now() + datetime.timedelta(seconds=10)
# t2 = datetime.datetime.combine(t, datetime.time.min)
def wrapper():
print('test')
schedule_next_email()
IOLoop.current().add_timeout(t, wrapper) # Error
schedule_next_email()
参考:
Traceback (most recent call last):
File "", line 1, in
File "", line 8, in schedule_next_email
File "C:\Users\...\anaconda3\lib\site-packages\tornado\ioloop.py", line 585, in add_timeout
raise TypeError("Unsupported deadline %r" % deadline)
TypeError: Unsupported deadline datetime.datetime(2021, 11, 11, 0, 47, 28, 944375)
add_timeout
采用 timedelta
,而不是 datetime
作为截止日期参数,因此只需直接传递 timedelta
:
from tornado.ioloop import IOLoop
import datetime
def schedule_next_email():
t = datetime.timedelta(seconds=10)
def wrapper():
print('test')
schedule_next_email()
IOLoop.current().add_timeout(t, wrapper)
schedule_next_email()
确保IO循环以一种或另一种方式开始;出于测试目的,我在末尾添加了 IOLoop.current().start()
,但生产代码可能会有所不同。
我正在尝试 运行 在 Tornado 中定期使用 IOLoop 的函数。但是,下面的代码会报错?
TypeError: Unsupported deadline datetime.datetime(2021, 11, 11, 0, 35, 49, 445200)
from tornado.ioloop import IOLoop
import datetime
def schedule_next_email():
# t = datetime.date.today() + datetime.timedelta(seconds=10)
t = datetime.datetime.now() + datetime.timedelta(seconds=10)
# t2 = datetime.datetime.combine(t, datetime.time.min)
def wrapper():
print('test')
schedule_next_email()
IOLoop.current().add_timeout(t, wrapper) # Error
schedule_next_email()
参考:
Traceback (most recent call last): File "", line 1, in File "", line 8, in schedule_next_email File "C:\Users\...\anaconda3\lib\site-packages\tornado\ioloop.py", line 585, in add_timeout raise TypeError("Unsupported deadline %r" % deadline) TypeError: Unsupported deadline datetime.datetime(2021, 11, 11, 0, 47, 28, 944375)
add_timeout
采用 timedelta
,而不是 datetime
作为截止日期参数,因此只需直接传递 timedelta
:
from tornado.ioloop import IOLoop
import datetime
def schedule_next_email():
t = datetime.timedelta(seconds=10)
def wrapper():
print('test')
schedule_next_email()
IOLoop.current().add_timeout(t, wrapper)
schedule_next_email()
确保IO循环以一种或另一种方式开始;出于测试目的,我在末尾添加了 IOLoop.current().start()
,但生产代码可能会有所不同。