티스토리 뷰
728x90
Django version : 4.2.4
OS ENV: ubuntu 22.04
DB ENV: ubuntu 22.04 postgresql
Celery version : 5.3.1
amqp : rmq
Django + Celery setting 삽질 기록
1. install django by pip3
$ pip install django
2. install celery with apt
$ sudo apt-get install celery
3. create django app with cmd
This is important which is repository structure:
app
- app1
+ models.py
+ tasks.py
- start_django
+ __init__.py
+ asgi.py
+ celery.py
+ settings.py
+ urls.py
+ wsgi.py
manage.py
we need to edit settings.py, __init__.py and create celery.py, models.py, tasks.py in app1 folder.
1. __init__.py
#__init__.py
# 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',)
2. celery.py
# celery.py
from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
# 기본 장고파일 설정
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'start_django.settings')
app = Celery('start_django')
app.config_from_object('django.conf:settings', namespace='CELERY')
#등록된 장고 앱 설정에서 task 불러오기
app.autodiscover_tasks()
@app.task(bind=True)
def debug_task(self):
print('Request: {0!r}'.format(self.request))
3. settings.py
"""
Django settings for start_django project.
Generated by 'django-admin startproject' using Django 4.2.4.
For more information on this file, see
https://docs.djangoproject.com/en/4.2/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/4.2/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.2/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'secret_key'
# 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',
# celery
'django_celery_beat',
'django_celery_results',
'app1'
]
# Celery
CELERY_BROKER_URL = 'amqp://localhost' # 로컬 테스트용
CELERY_RESULT_BACKEND = 'django-db'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
# settings.py
# Celery Configuration Options
CELERY_TIMEZONE = "Australia/Tasmania"
CELERY_TASK_TRACK_STARTED = True
CELERY_TASK_TIME_LIMIT = 30 * 60
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 = 'start_django.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 = 'start_django.wsgi.application'
# Database
# https://docs.djangoproject.com/en/4.2/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'dbname',
'USER': 'username',
'PASSWORD': 'p@ssword!',
'HOST': 'db address',
'PORT': '',
}
}
# Password validation
# https://docs.djangoproject.com/en/4.2/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.2/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.2/howto/static-files/
STATIC_URL = 'static/'
# Default primary key field type
# https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
4.tasks.py
from __future__ import absolute_import, unicode_literals
from celery import shared_task
@shared_task
def add(x, y):
return x + y
@shared_task
def mul(x, y):
return x * y
@shared_task
def xsum(numbers):
return sum(numbers)
Upper settings and editting gives proper working at celery use.
# runserver
python3 manage.py runserver
#at start_django path to run celery worker
celery -A start_django worker --loglevel=info
#at path where manage.py exist
python3 manage.py shell
>>import add from app1.tasks
>>result= add.delay(2,2)
>>result.ready()
>> True
>>result.get()
>> 4
누군가에게 도움이 되길 바랍니다.
'기술과 IT' 카테고리의 다른 글
[개발] celery-python 내장 함수 설명 (0) | 2023.08.22 |
---|---|
[개발]run ai models using docker with gpu use (0) | 2023.08.22 |
MS OneDrive 공유 적용 예제 (0) | 2023.07.15 |
AWS 람다 소개 (0) | 2023.07.06 |
Python OCR 라이브러리 종류와 장단점 (0) | 2023.07.05 |