celary.py and wsgi.py: os.environ.get("DJANGO_SETTINGS_MODULE")='proj.settings'

时间:2017-04-06 17:17:30

标签: django celery

Django 1.11

Celery 4.0.2

Let's read Celery's tutorial: http://docs.celeryproject.org/en/latest/django/first-steps-with-django.html#index-0

It says that we should place this line in celery.py:

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

Nnamely the documentation says:

You don’t need this line, but it saves you from always passing in the settings module to the celery program.

I just commented this line out in my IDE. And put a breakpoint after it.

And in the debugger I checked:

os.environ.get("DJANGO_SETTINGS_MODULE")='proj.settings'

Well, the key-value pair is already in the os.environ.

By the way, the same string is in wsgi.py:

os.environ.get("DJANGO_SETTINGS_MODULE")='proj.settings'

It is placed there by django-admin utility when startproject was executed.

So, this doubling the same string is definitely violation of DRY principle.

By the way, settings may be stored not in proj.settings. But in settings.local and settings.production (as Two Scoops of Django recommends).

So, these two strings: in celery.py and wsgi.py are very suspicious.

Could you comment: 1. What do we need them if the value is already mapped to the key in os.environ? 2. How not to violate the DRY principle here especially if we use settings.local and settings.production)? 3. So, what does the Celery's documentation mean by "always passing in the settings module to the celery program"?

1 个答案:

答案 0 :(得分:0)

  
      
  1. 如果值已经映射到os.environ中的键,我们需要它们什么?
  2.   

如果它已经在settings.py中设置了你再也不用担心芹菜了,那只是提醒你的一种做法。

  
      
  1. 如果我们使用settings.local和settings.production,如何不违反DRY原则?
  2.   

如果您有settings.localsettings.prod,通常的做法是在settings.basesettings.common中加入常用设置。因此,所有常见的设置,如BASE_DIR等,都会进入那里。

  
      
  1. 那么,Celery的文档是什么意思,总是将设置模块传递给芹菜程序"?
  2.   

您应该告诉celery从os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'proj.settings')app.config_from_object('django.conf:settings', namespace='CELERY')获取设置。

如果你做了那个,你可以忽略另一个,这意味着什么。