django使用celery

Posted a438842265

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了django使用celery相关的知识,希望对你有一定的参考价值。

一、基本使用

django_celery_demo
├── app01
│   ├── __init__.py
│   ├── admin.py
│   ├── apps.py
│   ├── migrations
│   ├── models.py
│   ├── tasks.py
│   ├── tests.py
│   └── views.py
├── db.sqlite3
├── django_celery_demo
│   ├── __init__.py
│   ├── celery.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── manage.py
├── red.py
└── templates
技术图片
import os
from celery import Celery

# set the default Django settings module for the ‘celery‘ program.
os.environ.setdefault(DJANGO_SETTINGS_MODULE, django_celery_demo.settings)

app = Celery(django_celery_demo)

# 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()
django_celery_demo/celery.py
技术图片
from .celery import app as celery_app

__all__ = (celery_app,)
django_celery_demo/__init__.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)
app01/tasks.py
技术图片
# ######################## Celery配置 ########################
CELERY_BROKER_URL = redis://10.211.55.20:6379
CELERY_ACCEPT_CONTENT = [json]
CELERY_RESULT_BACKEND = redis://10.211.55.20:6379
CELERY_TASK_SERIALIZER = json
django_celery_demo/settings.py
技术图片
from django.shortcuts import render, HttpResponse
from app01 import tasks
from django_celery_demo import celery_app
from celery.result import AsyncResult


def index(request):
    result = tasks.add.delay(1, 8)
    print(result)
    return HttpResponse(...)


def check(request):
    task_id = request.GET.get(task)
    async = AsyncResult(id=task_id, app=celery_app)
    if async.successful():
        data = async.get()
        print(成功, data)
    else:
        print(任务等待中被执行)

    return HttpResponse(...)
app01/views.py
技术图片
from django.conf.urls import url
from django.contrib import admin
from app01 import views

urlpatterns = [
    url(r^admin/, admin.site.urls),
    url(r^index/, views.index),
    url(r^check/, views.check),
]
django_celery_demo/urls.py

 

定时任务 就是在view里面用

def index(request):
    # result = tasks.add.delay(1, 8)

    ctime = datetime.datetime.now()
    utc_ctime = datetime.datetime.utcfromtimestamp(ctime.timestamp())

    s10 = datetime.timedelta(seconds=10)
    ctime_x = utc_ctime + s10

    # 使用apply_async并设定时间
    result = tasks.add.apply_async(args=[1, 3], eta=ctime_x)
    print(result)
    return HttpResponse(...)

 参考https://www.cnblogs.com/wupeiqi/articles/8796552.html

 

以上是关于django使用celery的主要内容,如果未能解决你的问题,请参考以下文章

celery beat 没有发送消息(使用 django-celery-beat)

Celery 在远程任务上使用 Django Result Backend

尝试使用 celery beat 在 django 中调度一个函数但给出错误

django-celery-beat 垃圾邮件到期任务

我在使用 Celery、Redis 和 Django 时遇到问题

django+celery+rabbitmq 编码错误和 sig-kill