# How to set up Celery in a Django project?

# Introduction:

This is the second part of this blog series where we are learning to use `Celery` in a **Django** project, check out the full series [here](https://blog.devjunction.in/series/learn-celery).

In this blog, we are going to learn how to set up `Celery` & our **Task broker** in a **Django** project.

We’ll be using `RabbitMQ` as our **task broker**.

# Installation:

## Installing & Running RabbitMQ:

As I said in the [previous blog](https://blog.devjunction.in/series/learn-celery), `RabbitMQ` is a task broker, which means it **stores** the tasks came from **Django** and passes it to `Celery` for further execution.

So to use `Celery` in **Django**, we have to run a `RabbitMQ` **server** to connect it with `Celery` instance.

- On Linux or Mac or any Unix based system, you have to install a package for `RabbitMQ` **server**, on **Ubuntu** the **package** name is `rabbitmq-server` :
    
    ```bash
    sudo apt-get install rabbitmq-server
    ```
    
    Once you are done with the installation, you can start the service:
    
    ```bash
    sudo systemctl enable rabbitmq
    sudo systemctl start rabbitmq
    ```
    
- On Windows based OS:
    
    Please follow this link for setting up the `RabbitMQ` **server** in your system.
    [https://www.rabbitmq.com/install-windows.html](https://www.rabbitmq.com/install-windows.html)
    

## Installing Celery:

Now just activate your **Django** project’s virtual environment, and install the `Celery`:

```bash
pip install celery
```

# Celery in Django:

So now that we have installed all the required **packages**, it's time to integrate `Celery` with our **Django** project. I am assuming that you have already up and running **Django** project (without `Celery`).

- Now the first step is to create a new file named `celery.py` just in the same folder where `settings.py` resides and paste this code inside:

**Note**: Few things I am assuming here is that, the **Django** project name is `myproject` you should use your project name instead.

```python
import os
from celery import Celery

# set the default Django settings module for the 'celery' program.
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")

# You can use anything as your Celery instance name, 
# I am using "myproject_celery" as my celery instance name
app = Celery("myproject_celery")

# 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.
app.autodiscover_tasks()
```

- Now the next step is to connect Celery with Django, to do that simply open the `myproject/__init__.py` file, just in the same folder where `settings.py` resides and paste this code inside:

```python
# 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",)
```

# Running Celery:

If you have followed all the steps above, you are ready to start your `Celery` instance, and to do that simply run this command:

```bash
celery -A myproject.celery worker -l info
```

**Note**: `myproject` is the **name** of our project, which we have used above in `celery.py` file.

This is it for this blog, in our next blog, we will create our first **asynchronous** **task** and see how we can run it using our current setup.

> ***For more such crispy blogs daily, follow Dev.Junction, subscribe to our newsletter and get notified.***
> 

## Social Links

- **LinkedIn:** [https://www.linkedin.com/in/mnamegaurav/](https://www.linkedin.com/in/mnamegaurav/)
- **YouTube:** [https://www.youtube.com/c/devjunction](https://www.youtube.com/c/devjunction)
- **Website:** [https://gaurav.devjunction.in/](https://gaurav.devjunction.in/)
- **GitHub:** [https://github.com/mnamegaurav](https://github.com/mnamegaurav)
- **Instagram:** [https://www.instagram.com/mnamegaurav/](https://www.instagram.com/mnamegaurav/)
- **Twitter:** [https://twitter.com/mnamegaurav](https://twitter.com/mnamegaurav)
