Django Running Celery with Supervisor Celery Configuration

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

CELERY

  1. Installation - pip install django-celery

  2. Add

  3. Basic project structure.

     - src/
       - bin/celery_worker_start # will be explained later on
       - logs/celery_worker.log
       - stack/__init __.py
       - stack/celery.py
       - stack/settings.py
       - stack/urls.py
       - manage.py
    
  4. Add celery.py file to your stack/stack/ folder.

     from __future__ import absolute_import
     import os
     from celery import Celery
     os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'stack.settings')
     from django.conf import settings  # noqa
     app = Celery('stack')
     app.config_from_object('django.conf:settings')
     app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
    
  5. to your stack/stack/__init__.py add following code:

     from __future__ import absolute_import
     from .celery import app as celery_app  # noqa
    
  6. Create a task and mark it for example as @shared_task()

     @shared_task()
     def add(x, y):
         print("x*y={}".format(x*y))
    
  7. Running celery worker "by hand":

    celery -A stack worker -l info if you also want to add



Got any Django Question?