18  Celery Async Tasks

19 Django와 Celery를 사용한 비동기 작업

Celery와 Redis를 Django 애플리케이션에 통합하기 위한 예제 프로젝트입니다. 이 저장소에는 실제 Python Django 및 Celery를 사용한 비동기 작업 튜토리얼에 대한 코드가 보관되어 있습니다.

19.1 설정(macOS)

프로젝트를 사용해 보려면 가상 환경을 설정하고 나열된 종속성을 설치하십시오.

$ python -m venv venv
$ source venv/bin/activate
(venv) $ python -m pip install -r requirements.txt

또한 시스템에 Redis를 설치해야 합니다.

$ brew install redis

모든 종속성을 설치한 후에는 동시에 실행해야 하는 세 가지 프로세스를 시작해야 합니다.

  1. 장고
  2. 레디스
  3. 셀러리

모두 실행하려면 세 개의 서로 다른 터미널 창을 열고 하나씩 시작하세요.

Django 앱:

(venv) $ python manage.py migrate
(venv) $ python manage.py runserver

Redis 서버:

$ redis-server

셀러리:

(venv) $ python -m celery -A django_celery worker -l info

세 프로세스가 모두 실행 중이면 localhost:8000/으로 이동하여 피드백 응답을 제출할 수 있습니다. Celery는 작업 집약적인 프로세스를 시뮬레이션하고 프로세스가 끝나면 이메일을 보냅니다. Celery 작업자가 실행 중인 터미널 창의 로그 스트림에 이메일 메시지가 표시되는 것을 볼 수 있습니다.

19.2 파일: source_code_final/django_celery/__init__.py

from .celery import app as celery_app

__all__ = ("celery_app",)

19.3 파일: source_code_final/django_celery/asgi.py

django_celery 프로젝트에 대한 ASGI 구성입니다.

이는 application이라는 모듈 수준 변수로 호출 가능한 ASGI를 노출합니다.

이 파일에 대한 자세한 내용은 다음을 참조하세요. https://docs.djangoproject.com/en/4.0/howto/deployment/asgi/

"""
ASGI config for django_celery project.

It exposes the ASGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/4.0/howto/deployment/asgi/
"""

import os

from django.core.asgi import get_asgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "django_celery.settings")

application = get_asgi_application()

19.4 파일: source_code_final/django_celery/celery.py

import os

from celery import Celery

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "django_celery.settings")
app = Celery("django_celery")
app.config_from_object("django.conf:settings", namespace="CELERY")
app.autodiscover_tasks()

19.5 파일: source_code_final/django_celery/settings.py

django_celery 프로젝트에 대한 Django 설정입니다.

Django 4.0.6을 사용하여 ’django-admin startproject’로 생성되었습니다.

이 파일에 대한 자세한 내용은 다음을 참조하세요. https://docs.djangoproject.com/en/4.0/topics/settings/

설정 및 해당 값의 전체 목록은 다음을 참조하세요. https://docs.djangoproject.com/en/4.0/ref/settings/

"""
Django settings for django_celery project.

Generated by 'django-admin startproject' using Django 4.0.6.

For more information on this file, see
https://docs.djangoproject.com/en/4.0/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/4.0/ref/settings/
"""

from pathlib import Path

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/4.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = (
    "django-insecure-26w#-h7$d@obw2g^r%&!$6cs4s+3^strd^ky8-3wb90*lg8e_i"
)

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",
    "feedback.apps.FeedbackConfig",
]

MIDDLEWARE = [
    "django.middleware.security.SecurityMiddleware",
    "django.contrib.sessions.middleware.SessionMiddleware",
    "django.middleware.common.CommonMiddleware",
    "django.middleware.csrf.CsrfViewMiddleware",
    "django.contrib.auth.middleware.AuthenticationMiddleware",
    "django.contrib.messages.middleware.MessageMiddleware",
    "django.middleware.clickjacking.XFrameOptionsMiddleware",
]

ROOT_URLCONF = "django_celery.urls"

TEMPLATES = [
    {
        "BACKEND": "django.template.backends.django.DjangoTemplates",
        "DIRS": [],
        "APP_DIRS": True,
        "OPTIONS": {
            "context_processors": [
                "django.template.context_processors.debug",
                "django.template.context_processors.request",
                "django.contrib.auth.context_processors.auth",
                "django.contrib.messages.context_processors.messages",
            ],
        },
    },
]

WSGI_APPLICATION = "django_celery.wsgi.application"


# Database
# https://docs.djangoproject.com/en/4.0/ref/settings/#databases

DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.sqlite3",
        "NAME": BASE_DIR / "db.sqlite3",
    }
}


# Password validation
# https://docs.djangoproject.com/en/4.0/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        "NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator",
    },
    {
        "NAME": "django.contrib.auth.password_validation.MinimumLengthValidator",
    },
    {
        "NAME": "django.contrib.auth.password_validation.CommonPasswordValidator",
    },
    {
        "NAME": "django.contrib.auth.password_validation.NumericPasswordValidator",
    },
]


# Internationalization
# https://docs.djangoproject.com/en/4.0/topics/i18n/

LANGUAGE_CODE = "en-us"

TIME_ZONE = "UTC"

USE_I18N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.0/howto/static-files/

STATIC_URL = "static/"

# Default primary key field type
# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"

# Email placeholder setting
EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend"

# Celery settings
CELERY_BROKER_URL = "redis://localhost:6379"
CELERY_RESULT_BACKEND = "redis://localhost:6379"

19.6 파일: source_code_final/django_celery/urls.py

django_celery URL 구성

‘urlpatterns’ 목록은 URL을 뷰로 라우팅합니다. 자세한 내용은 다음을 참조하세요. https://docs.djangoproject.com/en/4.0/topics/http/urls/ 예: 기능 보기 1. 가져오기 추가: my_app 가져오기 보기에서 2. urlpatterns에 URL을 추가합니다: path(’‘, views.home, name=’home’) 클래스 기반 보기 1. 가져오기 추가: from other_app.views import 홈 2. urlpatterns에 URL을 추가합니다: path(’‘, Home.as_view(), name=’home’) 다른 URLconf 포함 1. include() 함수를 가져옵니다. django.urls에서 include, path를 가져옵니다. 2. urlpatterns에 URL을 추가합니다: path(‘blog/’, include(‘blog.urls’))

"""django_celery URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/4.0/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""

from django.contrib import admin
from django.urls import include, path

urlpatterns = [
    path("admin/", admin.site.urls),
    path("", include("feedback.urls")),
]

19.7 파일: source_code_final/django_celery/wsgi.py

django_celery 프로젝트에 대한 WSGI 구성입니다.

application이라는 모듈 수준 변수로 호출 가능한 WSGI를 노출합니다.

이 파일에 대한 자세한 내용은 다음을 참조하세요. https://docs.djangoproject.com/en/4.0/howto/deployment/wsgi/

"""
WSGI config for django_celery project.

It exposes the WSGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/4.0/howto/deployment/wsgi/
"""

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "django_celery.settings")

application = get_wsgi_application()

19.8 파일: source_code_final/feedback/__init__.py

19.9 파일: source_code_final/feedback/admin.py

# from django.contrib import admin

# Register your models here.

19.10 파일: source_code_final/feedback/apps.py

from django.apps import AppConfig


class FeedbackConfig(AppConfig):
    default_auto_field = "django.db.models.BigAutoField"
    name = "feedback"

19.11 파일: source_code_final/feedback/forms.py

from django import forms
from feedback.tasks import send_feedback_email_task


class FeedbackForm(forms.Form):
    email = forms.EmailField(label="Email Address")
    message = forms.CharField(
        label="Message", widget=forms.Textarea(attrs={"rows": 5})
    )

    def send_email(self):
        send_feedback_email_task.delay(
            self.cleaned_data["email"], self.cleaned_data["message"]
        )

19.12 파일: source_code_final/feedback/migrations/__init__.py

19.13 파일: source_code_final/feedback/models.py

# from django.db import models

# Create your models here.

19.14 파일: source_code_final/feedback/tasks.py

from time import sleep

from celery import shared_task
from django.core.mail import send_mail


@shared_task()
def send_feedback_email_task(email_address, message):
    """Sends an email when the feedback form has been submitted."""
    sleep(20)  # Simulate expensive operation that freezes Django
    send_mail(
        "Your Feedback",
        f"\t{message}\n\nThank you!",
        "support@example.com",
        [email_address],
        fail_silently=False,
    )

19.15 파일: source_code_final/feedback/tests.py

# from django.test import TestCase

# Create your tests here.

19.16 파일: source_code_final/feedback/urls.py

from django.urls import path
from feedback.views import FeedbackFormView, SuccessView

app_name = "feedback"

urlpatterns = [
    path("", FeedbackFormView.as_view(), name="feedback"),
    path("success/", SuccessView.as_view(), name="success"),
]