APScheduler+tornado的使用:
# -*- coding: utf-8 -*-
"""
@Author : Pink
@Time : 12/29/2021 2:48 PM
@Function:
"""
import time
from tornado import web, ioloop
from tornado.httpserver import HTTPServertime_create_delta
from settings.settings import settings
from settings.urls import urls
from apscheduler.schedulers.tornado import TornadoScheduler
def hello():
print('hello')
def init_scheduler():
scheduler = TornadoScheduler(timezone='Asia/Shanghai')
# 定时每天7点0分0秒 执行一次hello函数 next_run_time的意思是从什么时候开始
# scheduler.add_job(hello, "cron", hour=7, minute=0, second=0, next_run_time=datetime.now())
scheduler.add_job(hello, "cron", hour=7, minute=0, second=0)
scheduler.start()
def make_app():
app = web.Application(urls, **settings)
return app
if __name__ == '__main__':
init_scheduler()
app = make_app()
server = HTTPServer(app)
server.bind(8081)
server.start(1)
ioloop.IOLoop.current().start()
使用apscheduler遇到的问题
PytzUsageWarning: The zone attribute is specific to pytz's interface; please migrate to a new time zone provider. For more details on how to do so, see https://pytz-deprecation-shim.readthedocs.io/en/latest/migration.html
if obj.zone == 'local':PytzUsageWarning: The normalize method is no longer necessary, as this time zone supports the fold attribute (PEP 495). For more details on migrating to a PEP 495-compliant implementation, see https://pytz-deprecation-shim.readthedocs.io/en/latest/migration.html
return self.timezone.normalize(dateval + difference), fieldnumPytzUsageWarning: The localize method is no longer necessary, as this time zone supports the fold attribute (PEP 495). For more details on migrating to a PEP 495-compliant implementation, see https://pytz-deprecation-shim.readthedocs.io/en/latest/migration.html
return self.timezone.localize(datetime(**values))
解决方法:添加时区
scheduler = TornadoScheduler(timezone='Asia/Shanghai')