Celery 3.1.9 Django integration, specifying settings file, without using djcelery

前端 未结 2 516
孤街浪徒
孤街浪徒 2021-01-03 03:22

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

相关标签:
2条回答
  • 2021-01-03 03:42

    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
    
    0 讨论(0)
  • 2021-01-03 03:46

    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

    0 讨论(0)
提交回复
热议问题