celery的作用
celery是一种异步框架,它的作用主要有两大块,利用python并发技术,帮我们高效地处理耗时异步任务,让不让主线程
等待,另外一个可以处理定时任务。如下图broker通常是存消息的中间见,user为生产者,通常生产者,有新消息时
会通常把消息扔到brkoker中间件间里,celery的worker就会不断地从中间件里取消息处理任务,workers可以用多个,
task result store 就是存储结果的地方,worker处理完之后就会把结果存在result store中。下面聊聊celry在django中的
应用
一、django中异步任务
1、在app下面创建tasks.py,文件名只能叫做tasks
2、在django_celery_pro/django_celry_pro/ 下面创建celery.py 文件名只能为celery.py
3、配置settings
4、把目录切换到第一层django_celry_pro下面,启动celery
django_celery_pro
├── app01
│ ├── init.py
│ ├── admin.py
│ ├── apps.py
│ ├── migrations
│ ├── models.py
│ ├── tasks.py
│ ├── tests.py
│ └── views.py
├── db.sqlite3
├── django_celery_pro
│ ├── init.py
│ ├── celery.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── manage.py
├── red.py
└── templates
django_celery_pro/celery.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import sys
import os
from celery import Celery
# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'django_stu.settings')
app = Celery('django_stu')
# 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.
# 确保settings.py中的对于celery的配置都是以CELERY开头
app.config_from_object('django.conf:settings', namespace='CELERY')
# Load task modules from all registered Django app configs.
# 自动发现在django中注册的app下的tasks.py文件
app.autodiscover_tasks()
django_celery_pro/init.py
# 这样可以直接从 django_celery_pro中 导出celery_app
from django_stu.celery import app as celery_app
__all__ = ('celery_app',)
app01/tasks.py
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_celey_pro/settings.py
.....
# ######################## Celery配置 ########################
# redis消息中间件 redis://:密码@host:ip/redis库
CELERY_BROKER_URL = 'redis://:1234@127.0.0.1:6379/0'
CELERY_ACCEPT_CONTENT = ['json']
# redis存储结果的中间件
CELERY_RESULT_BACKEND = 'redis://:1234@127.0.0.1:6379/1'
CELERY_TASK_SERIALIZER = 'json'
app01/views.py
from django.shortcuts import render, HttpResponse
from app01 import tasks
from django_celery_pro import celery_app
from celery.result import AsyncResult
# 执行异步函数
# 立马返回httpResponse("....")结果,tasks.add.delay的结果存在结果中间件里
def index(request):
result = tasks.add.delay(1, 8)
# 记录任务id,可以供下次结果查询到
task_id = result.id
print(task_id )
return HttpResponse(task_id )
# 根据传入的task_id从 结果中间件里获取数据
def check(request):
task_id &#