django celery redis practice

Keywords: Celery Redis Django Windows

Environmental preparation

Start redirs server under windows

  • redis-server.exe redis.windows.conf
    image.png

celery configuration

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

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

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

  • Add celery.py 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://127.0.0.1:6379/0')

# Configure celery using django's settings file
app.config_from_object('django.conf:settings')

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

  • Project init.py 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)/tasks.py:
  • As for task, it's not necessary to put all tasks in it. tasks.py It can be placed in other classes, just add @task to the function.
import time
from celery import task
@task
def add(a,b):
    print("This is the beginning of the task.")
    print(a+b)
    time.sleep(10)
    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):
    tasks.add.delay(1,2)
    result = {'code': 0, 'msg': 'This is a background task.'}
    return JsonResponse(result)

Reconfigure

  • Execute the following code in the manger.py 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 management.py celery-A celery worker--loglevel=info, please use the following command
celery -A djangoApi worker --pool=solo -l info
  • Start the project, Python manager.py runserver 0.0.0:8081

Operation item

  • Visit add


    image.png
  • View Key Logs

[tasks]
  . api.base.BaseViewTask.task_run
  . api.tasks.add

[2019-04-07 13:26:02,855: INFO/MainProcess] Connected to redis://127.0.0.1:6379/0
[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\django.py:202: 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

Other

  • 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