Django configures celery timing tasks

Install celery

  • Use redis+celery (using Ali Source or optionally not)
    pip install -i -U "celery[redis]"

Modify Django's settings configuration file

  • Add celery file
    Add a folder to the app peer directory, for example: service_celery
    Add file to the folder as follows:

    #!/usr/bin/env python
    from __future__ import absolute_import, unicode_literals
    import os
    from celery import Celery
    # set the default Django settings module for the 'celery' program.
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'settings')
    app = Celery('bank_service')
    # 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.
    def debug_task(self):
        print('Request: {0!r}'.format(self.request))
  • Configure settings file and add:

    # import djcelery
    # djcelery.setup_loader()
    CACHES = {
        "default": {
            "BACKEND": "django_redis.cache.RedisCache",
            "LOCATION": "redis://",
            "OPTIONS": {
               "CLIENT_CLASS": "django_redis.client.DefaultClient",
    REDIS_URL = CACHES['default']['LOCATION']

Using celery tasks

  • Creating Asynchronous Tasks
    Add a folder to the app peer directory, for example: service_tasks
    Add file to the folder as follows:

    from service_celery.celery import app
    import time
    def add_num(self, a, b):
        c = a+b
  • Calling asynchronous tasks

    from service_tasks.tasks import add_num
    a, b = 1, 2
    add_num.delay(a, b)

Start celery

  • start-up

    # . / venv_bank/bin/celery is my local celery path, or you can use celery directly.
    # After the worker, there are some configuration aspects, which can also be removed.
    ../venv_bank/bin/celery -A bank_service worker --loglevel=info  --max-tasks-per-child 500 --autoscale=8,4

Links to this article: Time Not Blogging - Django Configuring Ceery Timing Tasks

Tags: Celery Django Redis pip

Posted on Tue, 08 Oct 2019 00:51:29 -0700 by petersen313