The celery documentation helpfully gives an example of a configuration file, but it is less clear on where to put this file, or how to ensure that the worker reads this config file when it boots.
celery.py defines an object of type celery.Celery
called app
, which is what I use to decorate all my tasks. All of the tasks are in /myproj/tasks/
.
My directory structure is a bit like this:
/myproj/celery.py
/celeryconfig.py # my settings
/tasks/ # tasks go here
Is there something I can put into celery.py
which makes it load the celeryconfig.py
file, or do I need to specify it on the worker command-line arguments?
Here's what my config file looks like right now:
## Broker settings.
broker_url = "pyamqp://admin:ypass@mq:5672"
# List of modules to import when the Celery worker starts.
imports = ('stoneid.tasks',)
After reading your question, I have understood that you are confused about where to keep the config file and how to import those.
Celery has many ways to load config file. One of those is config_from_object.
That will be a simple python file and consist of celery configs.
For your example celeryconfig.py
will consists:
CELERY_BROKER_URL = 'redis://localhost:6379'
CELERY_RESULT_BACKEND = 'redis://localhost:6379'
and celery.py
will contains below code.
app = Celery('myproj')
default_config = 'myproj.celeryconfig'
app.config_from_object(default_config)
If you follow above instruction, you do not need to pass conf via command line.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With