Skip to content

Instantly share code, notes, and snippets.

@Lh4cKg
Last active January 23, 2020 06:12
Show Gist options
  • Save Lh4cKg/d17b5791563414ab3b1faabd0b5d5ebf to your computer and use it in GitHub Desktop.
Save Lh4cKg/d17b5791563414ab3b1faabd0b5d5ebf to your computer and use it in GitHub Desktop.
Installing And Configuration Django 1.10 && Celery 4.0.2 && RabbitMQ
1. django (latest version)
$ pip install django
2. celery (latest version)
$ pip install celery
first steps
docs: http://docs.celeryproject.org/en/latest/django/first-steps-with-django.html
install celery extension
$ pip install django-celery-beat
docs: http://docs.celeryproject.org/en/latest/django/first-steps-with-django.html#django-celery-beat
Add django_celery_beat to INSTALLED_APPS.
$ pip install django-celery-results
docs: http://docs.celeryproject.org/en/latest/django/first-steps-with-django.html#django-celery-results
Add django_celery_results to INSTALLED_APPS.
$ python manage.py migrate
celery.py
from __future__ import absolute_import, unicode_literals
import os
import sys
from celery import Celery
from django.conf import settings
# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'insurance.settings.staging')
app = Celery('insurance')
# Using a string here means the worker doesn't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
# should have a `CELERY_` prefix.
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))
__init__.py
from __future__ import absolute_import, unicode_literals
# 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']
staging.py
# add bottom CELERY config
CELERY_BROKER_URL = 'amqp://guest:guest@localhost:5672//'
CELERY_RESULT_BACKEND = 'django-db'
3. RabbitMQ
docs: http://docs.celeryproject.org/en/latest/getting-started/brokers/rabbitmq.html
installing on (Fedora/CentOS 7)
$ sudo yum install rabbitmq-server librabbitmq.x86_64 librabbitmq-devel librabbitmq-tools
$ sudo systemctl enable rabbitmq-server
$ sudo rabbitmq-server
you can also run it in the background by adding the -detached option (note: only one dash):
$ sudo rabbitmq-server -detached
status
$ sudo rabbitmqctl status
stop
$ sudo rabbitmqctl stop
4. Starting the worker process
$ celery -A insurance worker -l info -n worker@insurance
# enable event group {task}
$ celery control enable_events
5. Starting beat
$ celery -A insurance beat -S insurance.scheders.TestScheduler
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment