Configuring Settings and Options
To configure Celery settings in this project, you primarily interact with the Settings object, which is exposed as app.conf. This object manages the merging of user-defined configuration, default values, and environment variable overrides.
Defining Configuration Sources
The most common way to define settings is by using an object or a module. This allows you to keep your configuration separate from your application logic.
from celery import Celery
app = Celery('tasks')
# Load settings from a python module
app.config_from_object('myproject.celeryconfig')
# Or load from an environment variable that points to a module
# export CELERY_CONFIG_MODULE="myproject.celeryconfig"
app.config_from_envvar('CELERY_CONFIG_MODULE')
You can also pass configuration directly during the Celery app initialization:
app = Celery('tasks', broker='redis://localhost:6379/0', backend='redis://')
Accessing Settings
The app.conf object (an instance of celery.app.utils.Settings) provides multiple ways to retrieve configuration values.
Attribute and Dictionary Access
You can access settings directly as attributes or using dictionary-style keys.
# Attribute access (preferred for common settings)
broker = app.conf.broker_url
# Dictionary access
serializer = app.conf['task_serializer']
Namespaced Access
For settings organized into namespaces (like worker, task, or broker), you can use get_by_parts to retrieve values using a path-like structure.
# Equivalent to app.conf['worker_prefetch_multiplier']
prefetch = app.conf.get_by_parts('worker', 'prefetch_multiplier')
Environment Variable Overrides
The Settings object automatically prioritizes specific environment variables over values defined in your configuration files. The following properties in celery.app.utils.Settings check environment variables first:
| Property | Environment Variable |
|---|---|
broker_url | CELERY_BROKER_URL |
broker_read_url | CELERY_BROKER_READ_URL |
broker_write_url | CELERY_BROKER_WRITE_URL |
result_backend | CELERY_RESULT_BACKEND |
Inspecting Configuration
You can generate summaries of your current configuration, which is useful for debugging or logging.
# Get a dictionary of all non-default settings
changes = app.conf.table(with_defaults=False)
# Get a human-readable string of configuration changes
print(app.conf.humanize())
By default, sensitive information (settings containing strings like API, TOKEN, KEY, SECRET, PASS) is automatically masked when using table() or humanize(). To see the raw values, pass censored=False:
raw_config = app.conf.table(censored=False)
Introspecting Options
Every Celery setting is described by an Option object (defined in celery.app.defaults). You can use find_option to retrieve the metadata for a specific setting, including its default value and type.
# Find metadata for a specific setting
namespace, key, option = app.conf.find_option('task_always_eager')
print(f"Namespace: {namespace}") # e.g., 'task'
print(f"Default: {option.default}")
print(f"Type: {option.type}")
Troubleshooting and Best Practices
Avoid Mixing Key Formats
Celery supports both modern lowercase namespaced keys (e.g., task_always_eager) and legacy uppercase keys (e.g., CELERY_ALWAYS_EAGER). However, you must be consistent. Mixing both formats in the same configuration source will trigger an ImproperlyConfigured error during the detect_settings phase.
Configuration Finalization
The app.conf object is initially a PendingConfiguration proxy. It only loads the full configuration from your source when a key is first accessed. You can manually force this process using app.conf.finalize():
# Ensure all configuration sources are read and merged
app.conf.finalize()
Deprecated Settings
If you use legacy setting names, Celery will track them. You can check for and warn about deprecated settings using:
app.conf.maybe_warn_deprecated_settings()
This will issue a warning suggesting the modern alternative for each deprecated key found in your configuration.