0
点赞
收藏
分享

微信扫一扫

django定时任务

django定时任务

<br style="box-sizing: border-box;"/>

最近在做django项目时,需要在项目运行过程中运行定时任务,下面是调研的几种方法。

<br style="box-sizing: border-box;"/>

一、django-contab插件

1、安装:pip install django-crontab<br style="box-sizing: border-box;"/>

<br style="box-sizing: border-box;"/>

2、定时测试脚本:<br style="box-sizing: border-box;"/>

<br style="box-sizing: border-box;"/>

3、在settings.py上配置:<br style="box-sizing: border-box;"/>

INSTALLED_APPS = (

'django_crontab',

...

)

CRONJOBS = [<br style="box-sizing: border-box;"/> 

('*/1 * * * *','crontab_test.mycron.my_cron','>> '+os.path.join(BASE_DIR,'info.log')+' 2>&1')

]<br style="box-sizing: border-box;"/>

<br style="box-sizing: border-box;"/>

4、启动定时任务

说明:定时器一般只用于linux系统,linux本身带了crontab的定时任务功能

使用下面的命令将定时任务写入系统的crontab中,在系统中使用crontab –l可以看到

python ​​manage.py​​ crontab add<br style="box-sizing: border-box;"/>

删除定时任务命令

Python ​​manage.py​​ crontab remove

查看定时任务

Python ​​manage.py​​ crontab show

<br style="box-sizing: border-box;"/>

二、APScheduler

1、安装<br style="box-sizing: border-box;"/>

pip install apscheduler

2、在settings.py上配置:

INSTALLED_APPS = [<br style="box-sizing: border-box;"/> ......<br style="box-sizing: border-box;"/> 'django_apscheduler',#定时执行任务<br style="box-sizing: border-box;"/>]

3、执行迁移命令:

python ​​manage.py​​ migrate

<br style="box-sizing: border-box;"/>

4、使用<br style="box-sizing: border-box;"/>

<br style="box-sizing: border-box;"/>

5、启动定时任务

sched.start()

<br style="box-sizing: border-box;"/>

DON'T

WORRY

BE

HAPPY


举报

相关推荐

0 条评论