介绍:
APScheduler的全称是Advanced Python Scheduler。它是一个轻量级的 Python 定时任务调度框架。APScheduler 支持三种调度任务:固定时间间隔,固定时间点(日期),Linux 下的 Crontab 命令。同时,它还支持异步执行、后台执行调度任务。
import time
from abc import ABC
from tornado import web, ioloop
from tornado.web import RequestHandler, url
from apscheduler.schedulers.tornado import TornadoScheduler
from tornado.httpserver import HTTPServer
from datetime import datetime
def hello():
print('hello')
def init_scheduler():
scheduler = TornadoScheduler(timezone='Asia/Shanghai')
# 定时每天7点0分0秒 执行一次hello函数 next_run_time的意思是从什么时候开始加上这个会立即执行hello方法
# scheduler.add_job(hello, "cron", hour=7, minute=0, second=0, next_run_time=datetime.now())
scheduler.add_job(hello, "cron", hour=15, minute=29, second=0)
scheduler.start()
class CoinMoneyHomeHandler(RequestHandler, ABC):
async def post(self):
print(2222)
def make_app():
routes = [
(url(r"/hello", CoinMoneyHomeHandler))
]
app = web.Application(routes, debug=True)
return app
if __name__ == '__main__':
init_scheduler()
app = make_app()
server = HTTPServer(app)
server.bind(8081)
server.start(1)
ioloop.IOLoop.current().start()