Setting Celery on Django
## Install Celery
pip install celery
Buat File celery.py dengan lokasi yang sama dengan tempat setting.py
from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'nolsatu_courses.settings')
""" mengambil setting redis """
BASE_REDIS_URL = os.environ.get('REDIS_URL', 'redis://localhost:6379')
app = Celery('nolsatu_courses')
app.config_from_object('django.conf:settings', namespace='CELERY')
# Looks up for task modules in Django applications and loads them
app.autodiscover_tasks()
""" setting pengaturan broker """
app.conf.broker_url = BASE_REDIS_URL
@app.task(bind=True)
def debug_task(self):
print('Request: {0!r}'.format(self.request))
Edit file __init__.py
from __future__ import absolute_import, unicode_literals
from .celery import app as celery_app
__all__ = ('celery_app',)
Buat File Task pada setiap folder app karena task.py selain di folder app tidak akan terdeteksi oleh worker
from celery import shared_task
@shared_task
def testing_method():
print("testing method task")