Skip to content

Instantly share code, notes, and snippets.

@andkon
Created March 14, 2015 18:12
Show Gist options
  • Save andkon/e6c1e05796ab932e0ce1 to your computer and use it in GitHub Desktop.
Save andkon/e6c1e05796ab932e0ce1 to your computer and use it in GitHub Desktop.
Installing celery on django and heroku
# this is the init.py in your Project folder: proj/proj/__init__.py
from __future__ import absolute_import
from .celery import app as celery_app
# replace 'proj' with the name of your django project.
from __future__ import absolute_import
import os
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', 'proj.settings')
app = Celery('proj')
app.config_from_object('proj.celeryconfig')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
@app.task(bind=True)
def debug_task(self):
print('Request: {0!r}'.format(self.request))
#celeryconfig.py
try:
from local_celeryconfig import *
except ImportError:
BROKER_URL = 'amqp://asdf' #insert cloudAMQP url
BROKER_POOL_LIMIT = 1
BROKER_CONNECTION_TIMEOUT = 10
CELERYD_CONCURRENCY = 4
CELERY_RESULT_BACKEND = 'amqp://asdf' #insert cloudAMQP url
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_ACCEPT_CONTENT=['json']
CELERY_TIMEZONE = 'Canada/Eastern'
CELERY_ENABLE_UTC = True
pip install celery
heroku addons:add cloudamqp
heroku config | grep CLOUDAMQP_URL
^^ that is the URL to put in celeryconfig
#local_celeryconfig.py
BROKER_URL = 'amqp://localhost/'
CELERY_RESULT_BACKEND = 'amqp://'
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_ACCEPT_CONTENT = ['json']
CELERY_TIMEZONE = 'Canada/Eastern'
CELERY_ENABLE_UTC = True
CELERY_DISABLE_RATE_LIMITS = True
web: gunicorn proj.wsgi
celery: celery worker -A proj -l info -c 4
# in any app that you want celery tasks, make a tasks.py and the celery app will autodiscover that file and those tasks.
from __future__ import absolute_import
from celery import shared_task
from random import randint
import sys
@shared_task
def add_random_numbers():
rand_1 = randint(1,9)
rand_2 = randint(1,9)
total = rand_1 + rand_2
print total
sys.stdout.flush()
return total
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment