# CELERY

### Download  & install reddis

{% embed url="<https://github.com/tporadowski/redis/releases>" %}

### go to &#x20;

### Install django,celery redis

```bash
pip install django celery redis
```

**Create a Django Project**:

Run the following command to create a new Django project:

```bash
django-admin startproject project_name
```

**Navigate to the Project Directory**:

Move into the project directory:

```bash
cd project_name
```

**Create a Django App**:

Create a new app within the project:

```bash
python manage.py startapp app_name
```

### Link App With Project

**Create a URL Mapping**&#x20;

<br>

**app\_name/urls.py**

```python
# app_name/urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('', views.index, name='index'),
]

```

**app\_name/views.py**

```python
# app_name/views.py

from django.http import HttpResponse

def my_view(request):
    return HttpResponse("Hello from my view!")

```

**project settings.py**

```
INSTALLED_APPS = [
    'app_name'
]
```

**project urls.py**

```python
# project_name/urls.py

from django.contrib import admin
from django.urls import path, include  # Import the include function

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('app_name.urls')),  # Include the app's URLs
]
```

**Start the Development Server To Check**

```bash
python manage.py runserver
```

**প্রজেক্ট ডিরেক্টরির যেখানে settings.py ফাইল আছে সেখানে celery.py নামে একটি ফাইল তৈরী করি।**

```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', 'project_name.settings')

app = Celery('project_name',broker='redis://localhost:6379/0')  # Use the same name as your Django project

# This reads the configuration from the Django settings file.
app.config_from_object('django.conf:settings', namespace='CELERY')

# Load task modules from all registered Django app configs.
app.autodiscover_tasks()

@app.task(bind=True)
def debug_task(self):
    print('Request: {0!r}'.format(self.request))

```

**app ফোল্ডারে tasks.py নামে একটি ফাইল তৈরী করি**&#x20;

```python
# app_name/tasks.py

from celery import shared_task

@shared_task
def my_task(parameter):
    # Your task logic here
    return "Task completed"
```

**প্রজেক্ট ফোল্ডারে cmd ওপেন করে নিচের কমান্ড রান করি**

```bash
celery -A project_name worker -l info
```

**app ফোল্ডারে views.py নামে ফাইলটি নিচের মত করি**

```python
from django.shortcuts import render
from django.http import HttpResponse  # Import HttpResponse

from .tasks import my_task

def index(request):
    # ...
    result = my_task.delay(5)

    return HttpResponse("Task triggered successfully")  # Return an HttpResponse
```

**এবার চেক করি celery টাস্কটি কাজ করছে কিনা।**

<figure><img src="https://664948176-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FdX27l90MHtjXhtBupXys%2Fuploads%2F6acUCtKC7V0N975dmnOE%2Fimage.png?alt=media&#x26;token=4faecd02-ba19-48b6-b228-89f52110652c" alt=""><figcaption></figcaption></figure>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://olee-tech.gitbook.io/django/undefined-6/celery.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
