49👍
The bug appears if an exception is raised while parsing settings. Such as when we set Django’s SECRET_KEY
(or any other setting) via an environment variable:
SECRET_KEY = os.environ['SECRET_KEY']
To solve the problem you can switch back to:
SECRET_KEY = "asdfasdfasdf"
or use:
SECRET_KEY = os.environ.get('SECRET_KEY', '')
You can also find which setting caused the problem if you comment our the following line in celery.py
file and start the worker again:
app.config_from_object('django.conf:settings', namespace='CELERY')
4👍
I got the same error when one of the keys was missing in my config.json which was being loaded in settings.py (basically a missing key in settings.py).
Error text is totally irrelevant.
Hope that helps!
- [Django]-Django TypeError: 'RelatedManager' object is not iterable
- [Django]-How do I stop getting ImportError: Could not import settings 'mofin.settings' when using django with wsgi?
- [Django]-Is not JSON serializable
3👍
I would like to add two things:
-
This is also true when you load settings from any configuration file, not essentially django’s. The question is related purely to Celery.
-
Some explanation on origins of this cryptic error:
worker_state_db
is a setting with a default value, so you shouldn’t have to set it manually. An exception is raised because Settings
are just empty and don’t have any values, even the default ones. That said, we don’t have the default config loaded. Somehow in Celery, the exception from parsing (the original one which caused the problem) is not propagated to the stderr when starting a worker. Hence, you get a message which doesn’t tell you anything about a possible solution.
How to fix it?
For example, if you have celeryconfig.py
where your celery app module is placed and you load settings from there via:
app.config_from_object('path.to.your.celery.module.celeryconfig')
Check your whole celeryconfig.py
file for anything that could crash or cause the parser to crash (incompatible settings values?).
- [Django]-Fetching static files failed with 404 in nginx
- [Django]-Combining Django F, Value and a dict to annotate a queryset
- [Django]-From virtualenv, pip freeze > requirements.txt give TONES of garbage! How to trim it out?
1👍
I got this over and over while trying to configure celery with a dockerized django project. The solution was including env_file
in docker-compose.yaml:
celery:
...
env_file: .env
- [Django]-How do I execute raw SQL in a django migration
- [Django]-What the difference between using Django redirect and HttpResponseRedirect?
- [Django]-Non-database field in Django model
0👍
Celery (in general, not just in Django’s case) tries to first load config from it’s config file.
Error cause:
Celery loads config file, but ANY exception is raised. Celery will display this error message instead:
AttributeError: 'Settings' object has no attribute 'worker_state_db'
Very common example:
Let’s say your config file expects BROKER_PASSWORD
environment variable in your Celery config file.
broker_password = os.environ['BROKER_PASSWORD']
BUT!!!
You forgot to make env variable BROKER_PASSWORD
available… this code will therefore raise KeyError
.
This will confuse Celery, so above message will be shown.
- [Django]-How to make the foreign key field optional in Django model?
- [Django]-Race conditions in django
- [Django]-Is there a way to negate a boolean returned to variable?