如何在Django 中设置定时任务?

栏目: Python · 发布时间: 7年前

内容简介:Django 作为后端Web开发框架,有时候我们需要用到定时任务来或者固定频次的任务来执行某段代码,这时我们就要用到Celery了。Django中有一个中间件:Django-celeryPython 3.6Django为小于1.8版本

Django 作为后端Web开发框架,有时候我们需要用到定时任务来或者固定频次的任务来执行某段代码,这时我们就要用到Celery了。Django中有一个中间件:Django-celery

环境:

Python 3.6

Django为小于1.8版本

Celery为3.1版本

第一步安装:django-celery

pip install django-celery

第二步:配置celery和任务

创建测试django环境:

django-admin.py createproject test
django-admin.py startapp demo
 

创建好的项目布局如下:

- proj/
  - manage.py
  - proj/
    - __init__.py
    - celery.py
    - settings.py
    - urls.py
  - demo/
    - migrations
    - __init__.py
    - admin.py
    - apps.py
    - models.py
    - tasks.py
    - tests.py
    - views.py
 

2.1 配置celery.py文件

需要替换的内容,我都在对应的行后提示了,剩下的内容默认就好

创建test/test/celery.py文件,内容如下:

from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
 
# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'proj.settings')    # “proj.settings”替换为你的项目信息:test.settings
 
app = Celery('proj') # 这里的proj替换为你的项目名称:test
 
# Using a string here means the worker doesn't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
#   should have a `CELERY_` prefix.
app.config_from_object('django.conf:settings', namespace='CELERY')
 
# Load task modules from all registered Django app configs.
app.autodiscover_tasks()
 
 
@app.task(bind=True)
def debug_task(self):
    print('Request: {0!r}'.format(self.request))
 

2.2 配置项目的init.py中配置celery内容

打开test/test/__init_.py文件,添加内容:

from __future__ import absolute_import, unicode_literals
 
# This will make sure the app is always imported when
# Django starts so that shared_task will use this app.
from .celery import app as celery_app
 
__all__ = ('celery_app',)
 

2.3 在task.py中添加计划任务

编辑test/demo/task.py文件,添加计划任务,内容如下:

# Create your tasks here
from __future__ import absolute_import, unicode_literals
from celery import shared_task
 
 
@shared_task
def add(x, y):
    return x + y
 
 
@shared_task
def mul(x, y):
    return x * y
 
 
@shared_task
def xsum(numbers):
    return sum(numbers)
 

第三步:任务执行

运行django项目: python manage.py runserver

3.1 后台添加计划任务

访问“http://localhost:8000/admin/”,在celery的管理页面里,选择Periodic tasks,进行任务添加。选择对应的任务,设置定时或者周期时间

3.2 启动定时的celery服务

注意:celery依赖 redis 服务,需要提前运行redis服务:`redis-server`
 
# 以下两个命令在不同的 shell 窗口里执行,需要在django的目录下
python manager.py celery beat  -l info   #接收定时任务的命令
python manager.py celery worker -l info  #执行定时任务的命令,此shell窗口会看到任务的输入信息
 

3.3 启动单次的celery服务

注意:celery依赖redis服务,需要提前运行redis服务:`redis-server`
 
python manager.py shell    # 进到django的shell里
from demo.task import mul, xsum    # 导入task任务
a = mul()
b = xsum()
# 执行a, b会输出信息
a(1,2)
b(1)
 

欢迎大家关注我的公众号:

如何在Django 中设置定时任务?
#####         如果我的文档给您提供了帮助,请点击一下页面广告,给个支持! #####如有需要沟通的,请直接评论或者是邮件沟通。欢迎小主关注我的微信公众号:BigYoung成长之路,微信搜索可关注。如想交个朋友,可直接加入我的QQ群:615870353;或者加入我的知识星球:ID:45959491

BigYoung版权所有,转载请邮件(Mr_wang_yang@163.com)获取授权,并注明转自:http://www.bigyoung.cn/1096.html


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Impractical Python Projects

Impractical Python Projects

Lee Vaughan / No Starch Press / 2018-11 / USD 29.95

Impractical Python Projects picks up where the complete beginner books leave off, expanding on existing concepts and introducing new tools that you’ll use every day. And to keep things interesting, ea......一起来看看 《Impractical Python Projects》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

MD5 加密
MD5 加密

MD5 加密工具