欢迎光临散文网 会员登陆 & 注册

django定时任务

2022-08-02 18:40 作者:爱测软件测试  | 我要投稿

最近在做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

陪伴是最长情的告白

每日为你推送最in的测试技术

关注我哦


django定时任务的评论 (共 条)

分享到微博请遵守国家法律