2016년 6월 13일 월요일

Django에서 Celery 설정 | Celery with Django

pip packages

$ pip install celery

[project]/celery.py

import os
from celery import Celery
from django.conf import settings

os.environ.setdefault('DJANGO_SETTINGS_MODULE', '[project].settings')
app = Celery('[project]')
app.config_from_object('django.conf:settings')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)

[project]/settings.py

from datetime import timedelta

CELERY_TIMEZONE = 'UTC'
CELERYBEAT_SCHEDULE = {
    'schedule-30-seconds': {
        'task': 'helpers.tasks.sum_and_print',
        'schedule': timedelta(seconds=30),
        'args': (1, 2),
    }
}

helpers/__init__.py

(empty)

helpers/tasks.py

from celery import shared_task

@shared_task(time_limit=30)
def sum_and_print(a, b):
    result = a + b
    print(result)

How to execute

$ python manage.py runserver
$ exec celery -A [project] worker -B --loglevel INFO --purge

Read More

댓글 없음:

댓글 쓰기