Start celery worker throws "no attribute 'worker_state_db'"

run/celery worker
celery task
celery autodiscover_tasks
celery logging
celery shared_task
celery task delay
celery task id
celery config

When I am trying to start celery worker in Django app as:

celery -A myApp worker -l info

I get following error:

File "/home/alexander/.pyenv/versions/3.5.1/envs/myApp/lib/python3.5/site-packages/celery/utils/collections.py", line 134, in __getattr__
    type(self).__name__, k))

    AttributeError: 'Settings' object has no attribute 'worker_state_db'

If you know how to solve it please write your idea!

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')

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  Tasks¶. Tasks are the building blocks of Celery applications. A task is a class that can be created out of any callable. It performs dual roles in that it defines both what happens when a task is called (sends a message), and what happens when a worker receives that message.

I would like to add two things:

  1. This is also true when you load settings from any configuration file, not essentially django's. The question is related purely to Celery.

  2. 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?).

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  Celery is used in production systems to process millions of tasks a day. The aim is to be compatible with existing Python Celery implementation. That means you should be able to run a Java client with a Python worker or vice-versa. Tested with Python Celery 4.1. At the moment, this is a very alpha version. It can. execute a task; report result

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

When I am trying to start celery worker in Django app as: celery -A myApp worker -l info I get following error: File  Celery is started with below command. celery -A myproject worker --loglevel=debug --concurrency=3 -Q testqueue myproject.py as part of the master process was making some queries to mysql database before forking the worker processes. As part of query flow in main process, django ORM creates a sqlalchemy connection pool if it does not already exist.

I have a simple test code that runs successfully on Linux, but it won't run on my windows computer(haved tried on win10 and win7). When I tried  Simply wait at least 15 minutes after drinking your celery juice before consuming anything else. _ If you’re sensitive and 16 ounces is too much, start with a smaller amount and work your way up. _ Use organic celery whenever possible. If you’re using conventional celery, be sure to wash it especially well before juicing.

This means that celery starts at worker mode using our celery_client module, and it uses an info log level to work Let's see how to throw jobs at this worker. Plant the Celery in a Pot (Optional) If you want your celery to get bigger, plant it in a pot. First, to prevent soil from coming out, cover the drainage holes in the bottom of your pot with a piece of screening, coffee filter, or paper towel. Then, fill your pot with potting soil until it is about 2 inches below the rim.

celery.execute.apply(task, args, kwargs, **options)¶ Parameters: throw – Re-​raise task exceptions. Run a task asynchronously by the celery daemon(s). I think it gets a better start in the water (for up to a week) before setting it out…little roots will start to develop. I use a lot of celery in our cooking, and maybe I can save some on our food bill by re-growing celery through the spring and summer and have fun doing it! I have never grown celery before, so I am looking forward to trying it.

Comments
  • Remove the line app.config_from_object... didn't work to debug the problem, but changing SECRET_KEY = os.environ... worked perfectly. Thanks.
  • Strange, last time I had some variables which were loaded without get() and I managed to find each of them after commenting that line. Anyway, it is better to go through all the settings and inspect which of them are loaded with os.environ['xxxxx']
  • what if I need to put those variables in os.enviro ? @alexander
  • They are inside os environment. Just different method to get them out