Celery 3.1.9 Django集成,指定设置文件,不使用djcelery [英] Celery 3.1.9 Django integration, specifying settings file, without using djcelery
问题描述
我使用多个设置文件,我想知道是否有一个简单的方法指定在初始化芹菜工作者
时使用哪个设置文件?
使用djcelery,它非常简单,因为它使用manage.py命令。
我天真试图检查是否
settings.DEBUG
在celery.py文件中是正确的,但当然这是因为设置尚未加载而失败!
下一步是潜水进入django-celery来源并模拟他们在做什么,但在此之前,我希望有人找到了一个简单的方法来实现这一目标?
谢谢
解决方案是使用环境变量。
a href =http://celery.readthedocs.org/en/latest/django/first-steps-with-django.html#using-celer y-with-djangorel =nofollow noreferrer> celery.py
而不是
os.environ.setdefault('DJANGO_SETTINGS_MODULE','proj.settings')
使用
os.environ.setdefault('DJANGO_SETTINGS_MODULE','{0} .format(get_env_setting('DJANGO_SETTINGS_MODULE')))
With get_env_setting
定义为:
从os import environ
从django.core.exceptions import不正确的配置
#https://github.com/twoscoops/django-twoscoops-project/blob/develop/project_name/project_name/settings/production.py#L14-L21
def get_env_setting(setting):
获取环境设置或返回异常
try:
return environ [setting]
除了KeyError:
error_msg =设置%s env变量%setting
raise不正确的配置(error_msg)
你可以把它放在你的 settings.base.py
。
然后为每个环境设置 DJANGO_SETTINGS_MODULE
环境变量。例如。在生产环境中将其设置为 my_project.settings.production
。此变量可以通过将以下行添加到〜/ .bashrc中永久设置:
export DJANGO_SETTINGS_MODULE = my_project.settings.production
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
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
这篇关于Celery 3.1.9 Django集成,指定设置文件,不使用djcelery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!