What is celery?
Celery is a task queue implementation for Python web applications used to asynchronously execute work outside the HTTP request-response cycle.
What do I need?
- Python (3.6, 3.7, 3.8)
- Celery version 5.0. 5 runs on, Python (3.6, 3.7, 3.8)
- redis
Note: Celery uses “brokers” to pass messages between a Django Project and the Celery workers. In this tutorial, we will use Redis as the message broker.
Install Redis as a Celery “Broker”
- Clone the redis package to your local system by using give link
https://raw.githubusercontent.com/servicestack/redis-windows/master/downloads/redis-latest.zip
- Open any terminal then goto redis path where you cloned. then type redis-server.exe

This is the project layout:

- Goto project folder(celerytut) then create file called celery.py (projname/projname/celery.py). add below code into celery.py file
Note: where ever celerytut is there chnage to your project name.
import os from celery import Celery from django.conf import settings # set the default Django settings module for the 'celery' program. os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'celerytut.settings') app = Celery('celerytut') # 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') # Load task modules from all registered Django app configs. app.autodiscover_tasks(lambda: settings.INSTALLED_APPS) app.conf.beat_schedule = { 'add-every-2-seconds':{ 'task' : 'add_2_mumbers', 'schedule' : 3.0, } } @app.task(bind=True) def debug_task(self): print(f'Request: {self.request!r}')
Import your Celery app
To ensure that the Celery app is loaded when Django starts, add the following code into the init.py file that sits next to your settings.py file and add below code into init.py file.
from .celery import app as celery_app __all__ = ('celery_app',)
Create tasks.py file under project app. add below code into tasks.py file.

tasks.py from celery import shared_task @shared_task(name = "add_2_mumbers") def myTask(): print("this is the code should run every 3 seconds")
you can run this task based on time interval.
call the function from views.
views.py from django.shortcuts import render # Create your views here. from .tasks import myTask myTask.delay()
In settings add below code:
# CELERY STUFF BROKER_URL = 'redis://localhost:6379' CELERY_RESULT_BACKEND = 'redis://localhost:6379' CELERY_ACCEPT_CONTENT = ['application/json'] CELERY_TASK_SERIALIZER = 'json' CELERY_RESULT_SERIALIZER = 'json' CELERY_TIMEZONE = 'Africa/Nairobi'
Running Locally
With your Django App and Redis running, open two new terminal windows/tabs. In each new window, navigate to your project directory, activate your virtualenv, and then run the following commands (one in each window):
<strong>$ celery -A celerytut worker -l info
$ celery -A celerytut beat -l info</strong>
worker should run like this

Beat should run linke this

Final project structure should be like this:
