from flask import Flask from flask_apscheduler import APScheduler class Config(object): JOBS = [ { ‘id‘: ‘job1‘, ‘func‘: ‘__main__:job1‘, ‘args‘: (1, 2), ‘trigger‘: ‘interval‘, ‘seconds‘: 10 } ] def job1(a, b): print(str(a) + ‘ ‘ + str(b)) if __name__ == ‘__main__‘: app = Flask(__name__) app.config.from_object(Config()) scheduler = APScheduler() scheduler.init_app(app) scheduler.start() app.run()
date 日期触发: 一次性指定日期
# 2016-12-12运行一次job_function scheduler.add_job(job_function, ‘date‘, run_date=date(2016, 12, 12), args=[‘text‘]) # 2016-12-12 12:00:00运行一次job_function scheduler.add_job(job_function, ‘date‘, run_date=datetime(2016, 12, 12, 12, 0, 0), args=[‘text‘])
interval 间隔调度
# 每两个小时调一下job_function sched.add_job(job_function, ‘interval‘, hours=2)
Cron 触发
# job_function将会在6,7,8,11,12月的第3个周五的1,2,3点运行 sched.add_job(job_function, ‘cron‘, month=‘6-8,11-12‘, day=‘3rd fri‘, hour=‘0-3‘) # 截止到2016-12-30 00:00:00,每周一到周五早上五点半运行job_function sched.add_job(job_function, ‘cron‘, day_of_week=‘mon-fri‘, hour=5, minute=30, end_date=‘2016-12-31‘)
原文:https://www.cnblogs.com/easy-test/p/12764083.html