Django Celery设置

配置Django Celery

用到的组件

组件 用途说明 可替代方案 使用的版本
Redis 用作celery 消息中心 Celery官方推荐rabbitmq
celery 执行后台任务
MySQL 用作celery存储
celery-beat 执行定时任务 Django-crontab,django-apscheduler
Django Web

装单机redis,设置密码为S*kheFr5jG5

1
docker run -d --name devops_redis_6379  --restart always -p 6379:6379 -v /home/hcit/docker/devops_redis_6379:/data  -d redis redis-server --appendonly yes --requirepass S*kheFr5jG5

安装django-celery-results

1
pip install django-celery-results

修改settings.py

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'cmdb.apps.CmdbConfig',
    'itweb.apps.ItwebConfig',
    'django_celery_results', ## 添加这行
    'rest_framework',
]
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# Database
# https://docs.djangoproject.com/en/3.1/ref/settings/#databases
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'devops',
        'USER':'chaos',
        'PASSWORD':'password',
        'HOST':'192.168.11.33',
        'ssl_disabled':'True',
        'use_pure':'True',
        'PORT':'3306'
    }
}
## 添加如下celery配置
## CELERY Settings

broker_url = 'redis://:S*kELQ8heFr5jG5@192.168.11.33:6379/0'

# 设置存储结果的后台
# result_backend = 'redis://192.168.11.113:31001/0'
# result_backend = 'django-db'
CELERY_RESULT_BACKEND = 'django-db'
# CELERY_RESULT_BACKEND = 'redis://192.168.11.113:31001/0'
# 可接受的内容格式
accept_content = ["json"]
# 任务序列化数据格式
task_serializer = "json"
# 结果序列化数据格式
result_serializer = "json"
# 可选参数:给某个任务限流
# task_annotations = {'tasks.my_task': {'rate_limit': '10/s'}}

# 可选参数:给任务设置超时时间。超时立即中止worker
# task_time_limit = 10 * 60

# 可选参数:给任务设置软超时时间,超时抛出Exception
task_soft_time_limit = 10 * 60

CELERY_TIMEZONE='Asia/Shanghai'
# 可选参数:如果使用django_celery_beat进行定时任务
# beat_scheduler = "django_celery_beat.schedulers:DatabaseScheduler"

# 更多选项见
# https://docs.celeryproject.org/en/stable/userguide/configuration.html

在项目文件夹中创建celery.py

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File        : celery.py
@Desc        : None
@Time        : 2021/02/07 16:21:31
@Author      : Chaos
@Version     : 1.0
'''
# here put the import lib
from __future__ import absolute_import, unicode_literals
import os
from celery import Celery

# 指定Django默认配置文件模块
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'devops.settings')

# 为我们的项目devops创建一个Celery实例。这里不指定broker容易出现错误。
app = Celery('devops', broker='redis://:S*kELQ8heFr5jG5@192.168.11.33:6379/0')

# 这里指定从django的settings.py里读取celery配置
# app.config_from_object('django.conf:settings')
# Using a string here means the worker don'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')

# 自动从所有已注册的django app中加载任务
app.autodiscover_tasks()
# Celery 加载所有注册的应用
# app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)

# 用于测试的异步任务
@app.task(bind=True)
def debug_task(self):
    print('Request: {0!r}'.format(self.request))

修改项目文件夹中 _init_.py

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File        : __init__.py
@Desc        : None
@Time        : 2021/02/07 16:29:05
@Author      : Chaos
@Version     : 1.0
'''
# here put the import lib


# coding:utf-8
from __future__ import absolute_import, unicode_literals

# 引入celery实例对象
from .celery import app as celery_app
__all__ = ('celery_app',)

参考链接: https://github.com/celery/celery/blob/master/docs/django/first-steps-with-django.rst

updatedupdated2021-10-252021-10-25