django定时任务

django定时任务


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


一、django-contab插件

1、安装:pip install django-crontab


2、定时测试脚本:


3、在settings.py上配置:

INSTALLED_APPS = (

‘django_crontab’,

)

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

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

]


4、启动定时任务

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

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

python manage.py crontab add

删除定时任务命令

Python manage.py crontab remove

查看定时任务

Python manage.py crontab show


二、APScheduler

1、安装

pip install apscheduler

2、在settings.py上配置:

INSTALLED_APPS = [

‘django_apscheduler’,#定时执行任务
]

3、执行迁移命令:

python manage.py migrate


4、使用


5、启动定时任务

sched.start()


DON’T

WORRY

BE

HAPPY

更多技术文章


版权声明:本文为Tester_muller原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。