Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where should I put the Celery configuration file?

Tags:

python

celery

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',)
like image 835
Salim Fadhley Avatar asked Jan 28 '23 03:01

Salim Fadhley


1 Answers

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.

like image 149
Saiful Azad Avatar answered Jan 29 '23 17:01

Saiful Azad