I'm trying to run a function periodically using IOLoop in Tornado. However, the following code gets the error?
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()
Ref: How to start Tornado periodic callback at a specific time?
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
takes a timedelta
, not a datetime
, as the deadline argument, so just pass the timedelta
directly:
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()
Make sure the IO loop starts in one way or another; I added IOLoop.current().start()
to the end for testing purposes, though it may be different for production code.