django -- Celery implements asynchronous and timed tasks

1. Environment


djang==1.11.2  # 1.8, 1.9, 1.10No problem

celery-with-redis==3.0  # redis needs to be used as an intermediary service (Broker)
celery==3.1.25  # Installing the above will install automatically

django-celery==3.2.2  # Cellery plug-in, to achieve timed tasks

Cellery > = 4.0 will report an error in this environment. It is not recommended to use in this environment for the moment

2. Installation

pip install django==1.11.2 celery-with-redis==3.0 django-celery==3.2.2

3. Install Redis as a Broker (RabbitMQ is officially recommended, but the installation is troublesome)

Many tutorials, a little

4. New django project

Official documents

- Demo
  - Demo
  - app
    - migrations
  • Configure
    'djcelery',# Django cellery can define timing tasks in the admin background. Before starting, you need to migrate directly

BROKER_URL = 'redis://'

CELERYBEAT_SCHEDULER = 'djcelery.schedulers.DatabaseScheduler'  # Database scheduling
  • New file demo / demo /
from __future__ import absolute_import

import os

from celery import Celery

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

from django.conf import settings  # noqa

app = Celery('Demo')

# Using a string here means the worker will not have to
# pickle the object when using Windows.
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)

def debug_task(self):
    print('Request: {0!r}'.format(self.request))
  • New Demo/app/
from Demo.celery import app

def cus_task(*arg):
    print('This is a test task')
  • Edit Demo/app/
from django.shortcuts import render, HttpResponse

from .tasks import cus_task

def index(request):
    return HttpResponse("Test async task")
  • Start django and cellery
# django
python runserver

# celery
celery -A Demo worker -l debug

Configuring the cellery schedule in the admin background

  • To configure
# As set in, execute
python migrate djcelery
  • Log in to admin background for configuration
# Djcellery module list

Crontabs  # Same as linux crontab
Intervals  # interval
Periodic tasks  # Cycle task

Configure a periodic task content app.tasks.cus'task to execute every 5s with crontab or interval settings

  • Start django and cellery and check the logs
celery -A Demo worker -l debug

# Another window
celery -A Demo beat -l debug --max-interval=10  # Scan djcellery task every 10s

Tags: Celery Django Redis Python

Posted on Sun, 03 May 2020 23:20:52 -0700 by scheols