django -- Celery implements asynchronous and timed tasks

1. Environment

python==2.7

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
kombu==3.0.37
billiard==3.3.0.23

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
    setting.py
    wsgi.py
    urls.py
  - app
    - migrations
    models.py
    views.py
    ...
  • Configure settings.py
INSTALLED_APPS = (
    ...
    'app',
    'djcelery',# Django cellery can define timing tasks in the admin background. Before starting, you need to migrate directly
)


BROKER_URL = 'redis://127.0.0.1:6379/0'
BROKER_TRANSPORT = 'redis'

CELERYBEAT_SCHEDULER = 'djcelery.schedulers.DatabaseScheduler'  # Database scheduling
  • New file demo / demo / cellery.py
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.config_from_object('django.conf:settings')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)


@app.task(bind=True)
def debug_task(self):
    print('Request: {0!r}'.format(self.request))
  • New Demo/app/tasks.py
from Demo.celery import app

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

from .tasks import cus_task


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

# celery
celery -A Demo worker -l debug

Configuring the cellery schedule in the admin background

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

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

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