# Installing Celery and Rabbitmq for background Job in Django
### What is Celery
celery is a task queue that helps and manage task queue of background jobs. Jobs such as sending emails,scraping,crawling etc task are resource intensive and time consuming instead of user waiting for response he can delegate the job to queue worker like celery which does asynchronous job at background, celery is scalable and dependable for background orchestration of jobs.
### what is Rabbitmq
Rabbitmq is a message broker it helps in scaling the application by deploying the message queuing mechanism between two different applications, it offers temporary storage and it avoids data loss. RabbitMQ takes message from broker and delivers to consumer without loss
it works as a intermediary platform which ensures message is delivered to right consumer. RabbitMQ implements AMQP (Advanced message queuing protocol)
# Django Installation
You can follow regular installation process from django document
i would suggest to install Django in a virtual environment with venv
Here is the tree structure of my Django project
├── db.sqlite3
├── myproject
│ ├── apps
│ │ ├── api
│ │ │ ├── admin.py
│ │ │ ├── apps.py
│ │ │ ├── __init__.py
│ │ │ ├── migrations
│ │ │ ├── models.py
│ │ │ ├── tests.py
│ │ │ └── views.py
│ │ ├── __init__.py
│ │ ├── main
│ │ │ ├── admin.py
│ │ │ ├── apps.py
│ │ │ ├── __init__.py
│ │ │ ├── migrations
│ │ │ ├── models.py
│ │ │ ├── tests.py
│ │ │ └── views.py
│ │ └── __pycache__
│ │ └── __init__.cpython-310.pyc
│ ├── asgi.py
│ ├── __init__.py
│ ├── settings
│ │ ├── base.py
│ │ ├── local.py
│ │ ├── production.py
│ ├── static
│ │ ├── css
│ │ ├── img
│ │ ├── js
│ │ ├── scss
│ │ │ ├── bootstrap
│ │ └── webfonts
│ ├── templates
│ │ ├── base.html
│ │ ├── main
│ │ └── registration
│ ├── urls.py
│ └── wsgi.py
├── Makefile
├── manage.py
├── README.md
├── requirements.txt
└── tags
Above is my django project folder structure
#### Install Celery and rabbitmq
inside your django project
```
pip install celery
```
open a different terminal with ctl + shft + T i am using ubuntu server.
and install rabbitmq server with below command
`sudo apt-get install rabbitmq-server`
enable rabbitmq
```
sudo systemctl enable rabbitmq-server
```
once rabbitmq is enabled we have to start the service
`sudo systemctl start rabbitmq-server`
to check if rabbitmq-server status issue below command
`sudo systemctl status rabbitmq-server`
my terminal looks like this for above command

By now we have Django installed in venv with above tree structure(i have configued for my needs you can have your own structure or default structure also works fine ), Celery installed, Rabbitmq installed and started the service now we have to configure Django for celery to work lets go ahead and do it
Lets create tasks.py
i am storing tasks.py file in myprojects/apps/main/tasks.py
paste below code in tasks.py file, save and close.
```
from celery import shared_task
@shared_task(name='add')
def add(x, y):
return x + y
```
1. Ensure Celery can Find Your Tasks
> Ensure that your task functions are in a file that is importable from your Django settings module. According to Django best practices for organizing Django projects/apps, tasks.py should be at the same level as models.py and views.py for each app.
2. Add the app in settings.py
> In mycase i am adding 'myproject.apps.main' in INSTALLED_APPS of base.py which is in settings folder save and close the file
3. Create celery.py file and add settings of celery
> celery.py file should be placed in main folder structure(project structure ) not in app structure i.e, it should be placed in the same path as asgi.py and wsgi.py and paste below code in the file
```
from __future__ import absolute_import , unicode_literals
import os
from celery import Celery
from django.conf import settings
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")
app = Celery('myproject')
app.config_from_object("django.conf:settings", namespace="CELERY")
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
from myproject.apps.main import tasks
@app.task(bind=True)
def debug_task(self):
print('Request: {0!r}'.format(self.request))
```
4. add below code in __init__py
> In order to make sure that Celery app is loaded when Django starts, you need to import the Celery application in the __init__.py file of your project. your myproject/__init__.py' should look like below
```
from __future__ import absolute_import, unicode_literals
# This will make sure the app is always imported when
# Django starts so that shared_task will use this app.
from .celery import app as celery_app
__all__ = ('celery_app',)
```
5. Now Ensure your celery worker is running.
write below command in terminal of your django project to check if celery is working
```
celery -A myproject worker -l info
```

you can check under tasks you will see the tasks added in tasks.py is also visible here add task is visble .
thats how you integrate Celery and rabbitmq for django after integrating and testing my folder structure looks like below
├── db.sqlite3
├── myproject
│ ├── apps
│ │ ├── api
│ │ │ ├── __init__.py
│ │ │ ├── migrations
│ │ │ ├── models.py
│ │ │ ├── __pycache__
│ │ │ ├── tests.py
│ │ │ └── views.py
│ │ ├── __init__.py
│ │ ├── main
│ │ │ ├── admin.py
│ │ │ ├── apps.py
│ │ │ ├── __init__.py
│ │ │ ├── management
│ │ │ │ ├── __init__.py
│ │ │ ├── migrations
│ │ │ │ ├── __init__.py
│ │ │ ├── models.py
│ │ │ ├── tasks.py
│ │ │ ├── tests.py
│ │ │ └── views.py
│ ├── asgi.py
│ ├── celery.py
│ ├── settings
│ │ ├── base.py
│ │ ├── local.py
│ │ ├── production.py
│ ├── static
│ │ ├── css
│ │ ├── img
│ │ ├── js
│ │ ├── scss
│ │ │ ├── bootstrap
│ ├── templates
│ ├── urls.py
│ └── wsgi.py
├── Makefile
├── manage.py
├── README.md
├── requirements.txt
└── tags
You can check celery.py , tasks.py files in folder structure.