Celery 3.1.9 Django integration defining a configuration file without using djcelery

I started using celery 3.1.9 today with Django. This newer version has tighter integration with django, which eliminates the need for django-celery.

I use several settings files, and I was wondering if there was an easy way to specify which settings files to use during initialization celery worker?

With djcelery, this is pretty simple as it uses manage.py commands.

I naively tried to verify that

settings.DEBUG was true in the celery.py file, but of course it did not succeed because the settings have not yet been loaded!

The next step is to plunge into the source of django-celery and imitate what they do, but before that I was hoping that someone would find an easy way to achieve this?

thanks

+4
source share
2 answers

The solution is to use environment variables.

In celery.py

Instead

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

using

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

C is get_env_settingdefined 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 can put this in your settings.base.pyexample ..

Then set the environment variable DJANGO_SETTINGS_MODULEfor each environment. For example. Install it in my_project.settings.productiona production environment. This variable can be permanently set by adding the following line to ~ / .bashrc:

export DJANGO_SETTINGS_MODULE=my_project.settings.production
+5
source

When initializing a celery worker on the command line, simply set the environment variable before the celery command.

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

+1
source

Source: https://habr.com/ru/post/1527585/


All Articles