Settings¶
Process-wide configuration, sourced from environment variables. Settings cover the task consumer concurrency and timings, the broker and database connections, the HTTP client user agent, pagination defaults, the console backdoor and the stack sampler.
It can be imported from fluid.settings:
The environment is read the first time get_settings is called, not at import time, so an application can populate the environment before the first access. The instance is then cached for the lifetime of the process.
Environment variable names¶
Most fields are read from FLUID_<FIELD_NAME>, and names are case insensitive, so
FLUID_MAX_CONCURRENT_TASKS and fluid_max_concurrent_tasks both set
max_concurrent_tasks.
A few fields keep a conventional external name instead, with no prefix:
| Field | Environment variable |
|---|---|
app_name |
APP_NAME |
env |
PYTHON_ENV |
log_level |
LOG_LEVEL |
log_handler |
LOG_HANDLER |
python_log_format |
PYTHON_LOG_FORMAT |
database |
DATABASE |
redis_default_url |
REDIS_DEFAULT_URL |
redis_max_connections |
MAX_REDIS_CONNECTIONS |
Warning
The prefixed form does not work for the fields in the table above. Setting
FLUID_APP_NAME has no effect, the value is read from APP_NAME only.
The prefix itself can be changed with FLUID_ENV_PREFIX, which is read when
fluid.settings is imported, so it has to be set before the first import of the
library:
Derived defaults¶
Three values are computed after the environment is read, when they are not set explicitly:
broker_urlfalls back toredis_default_url, so pointingREDIS_DEFAULT_URLat a Redis instance is enough to move the task queue with it.http_user_agentfalls back topython/{app_name}.log_levelis upper cased, soLOG_LEVEL=infoandLOG_LEVEL=INFOare equivalent.
Reading settings in tests¶
get_settings caches its result, so a test that changes the environment has to clear the cache for the change to take effect:
import os
from fluid.settings import get_settings
os.environ["FLUID_MAX_CONCURRENT_TASKS"] = "1"
get_settings.cache_clear()
API reference¶
fluid.settings.Settings
¶
Bases: BaseSettings
Lazy application settings sourced from environment variables.
Settings are read from the environment the first time
get_settings is called, not at import time.
Access the resolved values either via the cached instance or, for backwards
compatibility, via upper-case module attributes (settings.APP_NAME),
both of which resolve lazily.
model_config
class-attribute
instance-attribute
¶
app_name
class-attribute
instance-attribute
¶
log_level
class-attribute
instance-attribute
¶
log_handler
class-attribute
instance-attribute
¶
python_log_format
class-attribute
instance-attribute
¶
python_log_format = Field(
default="%(asctime)s %(levelname)s %(name)s %(message)s",
validation_alias="PYTHON_LOG_FORMAT",
)
database
class-attribute
instance-attribute
¶
database = Field(
default="postgresql+asyncpg://postgres:postgres@localhost:5432/fluid",
validation_alias="DATABASE",
)
redis_default_url
class-attribute
instance-attribute
¶
redis_default_url = Field(
default="redis://localhost:6379",
validation_alias="REDIS_DEFAULT_URL",
)
max_concurrent_tasks
class-attribute
instance-attribute
¶
max_concurrent_tasks = Field(
default=5,
description="Maximum number of concurrent tasks per TaskConsumer",
)
redis_max_connections
class-attribute
instance-attribute
¶
default_pagination_max_limit
class-attribute
instance-attribute
¶
fluid.settings.get_settings
cached
¶
Return the process-wide Settings instance.
The instance is built on first call (reading the environment then) and
cached for the lifetime of the process. Call get_settings.cache_clear()
to force a re-read, which is mostly useful in tests.