1 安装包
pip install apscheduler==2.1.2
2 安装完成后,在django项目urls.py中增加以下内容
from apscheduler.scheduler import Scheduler
from myapp.views import hello
sched = Scheduler()
@sched.interval_schedule(seconds=60) # 表示间隔一分钟会执行函数
def mytask():
hello()
sched.start()
myapp/ views.py 文件
def hello():
print('hello')
3 启动django项目, 每隔一分钟会执行一次hello函数
python manage.py runserver
# 执行效果如下所示:
System check identified 1 issue (0 silenced).
Run 'python manage.py migrate' to apply them.
July 26, 2018 - 18:09:00
Django version 2.0.6, using settings 'myapp.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
hello
hello
hello
网友评论