[Fixed]-Active Django settings file from Celery worker

4👍

One idea could be to have Django load the correct settings file every time and let celery use the one Django is using. That’s how I’m doing it.

Say you have the project structure:

project/
    proj/
        settings.py
        urls.py
        ...

Replace with

project/
    proj/
        settings/
            __init__.py
            local.py
            production.py
            common_settings.py
            ...
        urls.py
        ...

Let common_settings.py be all settings shared between all environments and let your init.py load whatever config should be used.

# __init__.py:

from common_settings import *

# A check for environment variable, hostname, etc:
# Example for checking hostname:
from platform import node
if node() in ['dev1', 'dev2']:
    from local import *
elif node() in ['prod1', 'prod2']:
    from production import *

Now you can always rely on project.proj.settings being the right settings file for your environment.

👤olofom

41👍

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

3👍

setdefault() returns the value of the variable in the system environment. It will return "project.settings.server" only if DJANGO_SETTINGS_MODULE is not defined.

So, I would leave the most frequently used settings module in there and change it when needed by explicitly declaring the environment variable:

i.e. for local development, in your virtualenv hooks, in your .bashrc, manually, etc:

export DJANGO_SETTINGS_MODULE=project.settings.local.

-1👍

found this buried in the celery docs:

# project/app/tasks.py
app = Celery()
app.config_from_object('django.conf:settings')

http://celery.readthedocs.org/en/latest/whatsnew-3.1.html#django-supported-out-of-the-box

This will simply pull celery settings from the django settings file, which is usually what you want for environment separation.

Leave a comment