5

I began using celery 3.1.9 today with Django. This newer version has a tighter integration with django that removes the need to using django-celery.

I use multiple settings files and I was wondering if there was an easy way to specify which settings file to use when initializing the celery worker?

With djcelery it is quite simply since it uses the manage.py commands.

I naively tried to check if

settings.DEBUG was true in the celery.py file, but of course that failed because the settings were not loaded yet!

THe next step is to dive into the django-celery source and emulate what they are doing, but before that, I was hoping someone had found an easy way of achieving this?

Thank you

2
  • Have you read the Getting started with Django? It has changed since when django-celery was in use. celery.readthedocs.org/en/latest/django/… Commented Feb 26, 2014 at 23:32
  • 2
    @oloform: Yes, the docs only explain how to setup when you have one settings file. Commented Mar 3, 2014 at 14:19

2 Answers 2

7

A solution is to use environment variables.

In celery.py

Instead of

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'proj.settings')

use

os.environ.setdefault('DJANGO_SETTINGS_MODULE', '{0}'.format(get_env_setting('DJANGO_SETTINGS_MODULE')))

With get_env_setting defined as:

from os import environ
from django.core.exceptions import ImproperlyConfigured

# https://github.com/twoscoops/django-twoscoops-project/blob/develop/project_name/project_name/settings/production.py#L14-L21
def get_env_setting(setting):
    """ Get the environment setting or return exception """
    try:
        return environ[setting]
    except KeyError:
        error_msg = "Set the %s env variable" % setting
        raise ImproperlyConfigured(error_msg)

You could put that in your settings.base.py e.g. .

Then set the DJANGO_SETTINGS_MODULE environment variable for each environment. E.g. set it to my_project.settings.production in the production environment. This variable can by permanently set by adding the following line to ~/.bashrc:

export DJANGO_SETTINGS_MODULE=my_project.settings.production
Sign up to request clarification or add additional context in comments.

2 Comments

This solution is not working for me, It throws the error django.core.exceptions.ImproperlyConfigured: Set the DJANGO_SETTINGS_MODULE env variable
Seems like you didn't set the DJANGO_SETTINGS_MODULE environment variable (point to your settings module). Also, this is just an idea for one implementation, the idea is about using an environment variable to decide what settings module to load.
1

When initializing the celery worker on the command line, just set the environment variable prior to the celery command.

DJANGO_SETTINGS_MODULE='proj.settings' celery -A proj worker -l info

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.