1、app.__init__.py
from flask import Flask from flask_apscheduler import APScheduler import config
# 初始化app app = Flask(config.APP) app.config.from_object('config') # # 初始化定时任务模块 scheduler = APScheduler() scheduler.init_app(app) scheduler.start()
2、config.py
# 定时模块配置 from utils.scheduler_test import test, test2, test3 SCHEDULER_API_ENABLED = True SCHEDULER_JOBS = [ { 'id': 'test', 'func': test, 'args': '', 'trigger': { 'type': 'cron', 'day_of_week': "0-6", 'hour': '*', 'minute': '1', 'second': '0' } }, { 'id': 'test2', 'func': test2, 'args': '', 'trigger': 'interval', 'seconds': 5 }, { 'id': 'test3', 'func': test3, 'args': '', 'trigger': 'interval', 'seconds': 15 } ]
3、scheduler_test.py
import datetime def test(): print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")) print("hello yangwenxiao") def test2(): print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")) print("hello yangwenxiao 2") def test3(): print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + "暂停任务") """ 暂停任务 """ # scheduler.scheduler.pause_job('test2') """ 恢复任务 """ # scheduler.resume_job('interval_task') """ 删除任务 """ # scheduler.remove_job('interval_task')