Skip to content

Instantly share code, notes, and snippets.

@alb3rto269
Last active August 29, 2015 14:08
Show Gist options
  • Save alb3rto269/add08acc212af9f1ac83 to your computer and use it in GitHub Desktop.
Save alb3rto269/add08acc212af9f1ac83 to your computer and use it in GitHub Desktop.
celery 3.1 + django-configurations
/mysite
| app1/
| models.py
| tasks.py
| app2/
| models.py
| tasks.py
| config/
| local.py
| development.py
| production.py
| settings.py
| celery.py
| manage.py
from __future__ import absolute_import
import os
from celery import Celery
from django.conf import settings
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings')
os.environ.setdefault('DJANGO_CONFIGURATION', 'Local')
from configurations import importer
importer.install()
app = Celery('mysite')
app.config_from_object('django.conf:settings')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
@app.task(bind=True)
def debug_task(self):
print('Request: {0!r}'.format(self.request))
from configurations import Configuration
class Local(Configuration):
...
INSTALLED_APPS = (
...
'app1',
'app2'
...
)
...
# CELERY CONFIGURATION
CELERY_REDIRECT_STDOUTS_LEVEL = 'DEBUG'
BROKER_URL = 'redis://localhost:6379/0'
CELERY_RESULT_BACKEND = 'redis://localhost:6379/0'
# END CELERY CONFIGURATION
from __future__ import absolute_import
from .local import Local
from .development import ev
from .production import Prod
...
from config.celery import app as celery_app
@celery_app.task
def hello_world():
print('Hello World')
@alb3rto269
Copy link
Author

  • To start celery workers run from the django root directory (config is the path where lives celery.py)

    celery -A config worker --loglevel=info
  • To run tasks:

    hello_word.delay()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment