首页 > 其他分享 >django中celery的使用

django中celery的使用

时间:2023-03-27 21:22:23浏览次数:76  
标签:tasks settings py django celery proj 使用

创建django项目

$ django-admin startproject proj
$ cd proj
$ tree
.
├── manage.py
└── proj
    ├── __init__.py
    ├── asgi.py
    ├── settings.py
    ├── urls.py
    └── wsgi.py

项目引入celery

创建一个新的 proj/proj/celery.py模块,定义celery实例:

文件:proj/proj/celery.py

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.
# 这里会加载多有的task
app.autodiscover_tasks()

将proj/proj/init.py 中导入模块,这样可以确保在django启动的时候加载应用。

文件 proj/proj/init.py

from .celery import app as celery_app
__all__=('celery_app',)

文件 proj/proj/settings.py

import os

# ^^^ The above is required if you want to import from the celery
# library.  If you don't have this then `from celery.schedules import`
# becomes `proj.celery.schedules` in Python 2.x since it allows
# for relative imports by default.

# celery设置
# 1. backend_url
CELERY_BROKER_URL = 'amqp://guest:guest@localhost'

#: Only add pickle to this list if your broker is secured
#: from unwanted access (see userguide/security.html)
CELERY_ACCEPT_CONTENT = ['json']
# celery backend 地址
CELERY_RESULT_BACKEND = 'db+sqlite:///results.sqlite'
# celery backend 序列化类型
CELERY_TASK_SERIALIZER = 'json'


"""
Django settings for proj project.
Generated by 'django-admin startproject' using Django 2.2.1.
For more information on this file, see
https://docs.djangoproject.com/en/2.2/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.2/ref/settings/
"""


# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'l!t+dmzf97rt9s*yrsux1py_1@odvz1szr&6&m!f@-nxq6k%%p'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'demoapp',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'proj.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'proj.wsgi.application'


# Database
# https://docs.djangoproject.com/en/2.2/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}


# Password validation
# https://docs.djangoproject.com/en/2.2/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/2.2/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.2/howto/static-files/

STATIC_URL = '/static/'

tasks文件项目中的位置

tasks文件位置一般有以下几种方案

方案一: 放到对应到app目录下面

├── app
│   ├── __init__.py
│   ├── admin.py
│   ├── apps.py
│   ├── migrations
│   │   └── __init__.py
│   ├── models.py
│   ├── tasks.py   # 创建一个task文件, 这里是worker相关代码
│   ├── tests.py
│   └── views.py
├── manage.py
└── proj
    ├── __init__.py
    ├── asgi.py
    ├── settings.py
    ├── celery.py  # celery 声明文件
    ├── urls.py
    └── wsgi.py

方案二: 单独创建一个task路径

├── celery_tasks
│   ├── __init__.py
│   ├── app01
│   │   ├── __init__.py
│   │   └── tasks.py   # app01相关tasks代码
│   ├── app02
│   │   ├── __init__.py
│   │   └── tasks.py   # app02相关tasks代码
│   └── app03
│       ├── __init__.py
│       └── tasks.py   # app03相关tasks代码
├── manage.py
└── proj
    ├── __init__.py
    ├── asgi.py
    ├── settings.py
    ├── celery.py # celery声明文件
    ├── urls.py
    └── wsgi.py

方案三: 直接在项目目录下创建

├── manage.py
├── proj
│   ├── __init__.py
│   ├── asgi.py
│   ├── settings.py
│   ├── celery.py # celery 声明文件
│   ├── urls.py
│   └── wsgi.py
└── tasks.py # task任务文件

编辑tasks文件

这里我们按照方案2的结构进行处理,目录为如下结构
假设 app1 为算数运算的服务, app2 为邮件服务

- app1/  
    - tasks.py   # 算数运算
    - models.py
- app2/
    - tasks.py   # 发送邮件
    - models.py

文件: app1/tasks.py

from celery import app
from app1.models import Widget


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


@app.task
def mul(x, y):
    return x * y


@app.task
def xsum(numbers):
    return sum(numbers)


@app.task
def count_widgets():
    return Widget.objects.count()


@app.task
def rename_widget(widget_id, name):
    w = Widget.objects.get(id=widget_id)
    w.name = name
    w.save()

文件 app2/tasks.py

from celery import app
import time
@app.task
def sendmail():
    print('sendmail mail to yunweigo')
    time.sleep(5.0)
    print('mail sent success')
    

通过数据库保存结果

通过数据库保存 backends 存储数据

django-celery-results - Using the Django ORM/Cache as a result backend¶
pip install django-celery-results
INSTALLED_APPS = (
    ...,
    'django_celery_results',
)
$ python manage.py migrate django_celery_results
Configure Celery to use the django-celery-results backend.

Assuming you are using Django’s settings.py to also configure Celery, add the following settings:

CELERY_RESULT_BACKEND = 'django-db'
For the cache backend you can use:

CELERY_RESULT_BACKEND = 'django-cache'
We can also use the cache defined in the CACHES setting in django.

# celery setting.
CELERY_CACHE_BACKEND = 'default'

# django setting.
CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
        'LOCATION': 'my_cache_table',
    }
}

启动过服务

$ celery -A proj worker -l INFO

 

 

转载自:https://blog.csdn.net/yunweigo/article/details/115267014

 

 

标签:tasks,settings,py,django,celery,proj,使用
From: https://www.cnblogs.com/shaoyishi/p/17262934.html

相关文章