Celery 4.0 supports Django 1.8 and newer versions. Please use Celery 3.1 for versions older than Django 1.8


pip install celery   #celery
pip install django-celery-beat #celery 具有管理界面的数据库支持的定期任务
pip install django-celery-results  #celery work orm存储结构
pip install flower  #celery监控


  1. 配置
    • install_app 新增django_celery_beat和django_celery_results
    • celery配置
         # celery
         CELERY_BROKER_URL = 'redis://'
         CELERY_BEAT_SCHEDULER = 'django_celery_beat.schedulers:DatabaseScheduler'
         # CELERY_RESULT_BACKEND = 'redis://'
         CELERY_ACCEPT_CONTENT = ['json']
         CELERY_TASK_SERIALIZER = 'json'
         CELERY_TIMEZONE = 'Asia/Shanghai'
         # CELERY_IMPORTS = ('tasks',)
    • 同步数据库
          python makemigrations
          python migrate
  2. 项目目录下创建 : file: proj/proj/

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')

app = Celery('proj')

# 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.

def debug_task(self):
    print('Request: {0!r}'.format(self.request))
  1. 项目的 文件中新增以下配置: proj/proj/
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',)
  1. 在app下面创建
- app1/
- app2/
  1. 创建task
# Create your tasks here
from __future__ import absolute_import, unicode_literals
from celery import shared_task

def add(x, y):
    return x + y

def mul(x, y):
    return x * y

def xsum(numbers):
    return sum(numbers)
  1. 启动
celery -A proj worker -l info
celery -A proj beat -l info
elery flower -A devops --address= --port=5555