在Django项目中增加定时器
使用调度框架APScheduler,
1. 安装APScheduler:
pip install apscheduler
2. 安装完成后,打开django web 项目的urls.py 增加以下内容:
from apscheduler.schedulers.background import BackgroundScheduler from datetime import datetime def tick(): print('Tick! The time is: %s' % datetime.now()) scheduler = BackgroundScheduler() scheduler.add_job(tick, 'interval', seconds=60) scheduler.start()
3.启动django 项目, 观察控制台,每60秒会有一个Tick输出:
System check identified no issues (0 silenced). December 08, 2019 - 20:34:23 Django version 3.0, using settings 'dispy.settings.stg' Starting development server at http://127.0.0.1:8000/ Quit the server with CTRL-BREAK. Tick! The time is: 2019-12-08 20:35:22.785887 Tick! The time is: 2019-12-08 20:36:22.786051
——————————————