Created
March 14, 2015 18:12
-
-
Save andkon/e6c1e05796ab932e0ce1 to your computer and use it in GitHub Desktop.
Installing celery on django and heroku
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pip install celery | |
heroku addons:add cloudamqp | |
heroku config | grep CLOUDAMQP_URL | |
^^ that is the URL to put in celeryconfig |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
web: gunicorn proj.wsgi | |
celery: celery worker -A proj -l info -c 4 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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