django celery redis practice

Keywords: Celery Redis Django Windows

Environmental preparation

Start redirs server under windows

  • redis-server.exe

celery configuration

  • Project file modification:
# celery settings
# celery intermediary redis://redis service ip address: port/database number
BROKER_URL = 'redis://'
# celery results are returned and can be used to track results

# The formatting of celery content and other messages
CELERY_ACCEPT_CONTENT = ['application/json', ]

# celery time zone settings, using the same time zone as TIME_ZONE in settings

  • Add file under the project folder:
# coding:utf-8
from __future__ import absolute_import, unicode_literals

from celery import Celery
from django.conf import settings
import os

# Get the current folder name, which is the project name of the Django
project_name = os.path.split(os.path.abspath('.'))[-1]
project_settings = '%s.settings' % project_name

# Setting environment variables
os.environ.setdefault('DJANGO_SETTINGS_MODULE', project_settings)

# Instantiate Celery. There are many tutorials on the Internet that fail to start without broker.
app = Celery('tasks', broker='redis://')

# Configure celery using django's settings file

# Celery loads all registered applications
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)

  • Project file modification:
# Introducing celery instance object
from __future__ import absolute_import, unicode_literals
from .celery import app as celery_app
__all__ = ['celery_app]

Test code

  • For example, in app01 (project name)/
  • As for task, it's not necessary to put all tasks in it. It can be placed in other classes, just add @task to the function.
import time
from celery import task
def add(a,b):
    print("This is the beginning of the task.")
    print("This is the end of the mission.")
  • The url configuration is as follows:
    path('add', views.add, name="add")
  • view code
from . import tasks
def add(request,*args,**kwargs):
    result = {'code': 0, 'msg': 'This is a background task.'}
    return JsonResponse(result)


  • Execute the following code in the directory. Note that most of the commands executed by the data on the network have problems, causing startup errors, such as this one is wrong, Python celery-A celery worker--loglevel=info, please use the following command
celery -A djangoApi worker --pool=solo -l info
  • Start the project, Python runserver 0.0.0:8081

Operation item

  • Visit add

  • View Key Logs

  . api.base.BaseViewTask.task_run
  . api.tasks.add

[2019-04-07 13:26:02,855: INFO/MainProcess] Connected to redis://
[2019-04-07 13:26:02,869: INFO/MainProcess] mingle: searching for neighbors
[2019-04-07 13:26:03,911: INFO/MainProcess] mingle: all alone
[2019-04-07 13:26:03,926: WARNING/MainProcess] e:\app\python35\lib\site-packages\celery\fixups\ UserWarning: Using settings.DEBUG leads to a memory leak, never use this setting in production environments!
  warnings.warn('Using settings.DEBUG leads to a memory leak, never '
[2019-04-07 13:26:03,926: INFO/MainProcess] celery@PC-20181208QWQO ready.
[2019-04-07 13:29:56,889: INFO/MainProcess] Received task: api.tasks.add[9fd98fd0-50ae-427f-8f33-52d1e4b43068]
[2019-04-07 13:29:56,894: WARNING/MainProcess] This is the beginning of the task.
[2019-04-07 13:29:56,895: WARNING/MainProcess] 3
[2019-04-07 13:30:06,896: WARNING/MainProcess] This is the end of the mission.
[2019-04-07 13:30:06,898: INFO/MainProcess] Task api.tasks.add[9fd98fd0-50ae-427f-8f33-52d1e4b43068] succeeded in 10.0s: None


  • How to combine front-end, such as ajax?
    • I added an extension field to the table. After each time a time-consuming task is performed, the page button judges the value of the extension field.
    • For example, 1 indicates that the execution is in progress, 0 indicates no execution, 2 indicates that the execution is completed to determine whether the button can be clicked again, etc.

Posted by stuckwithcode on Sun, 07 Apr 2019 19:33:32 -0700