2021年3月17日星期三

Why does documentation use lru_cache decorated function to get settings, instead of importing an initialized settings object from the module directly

FastAPI documentation recommends using lru_cache decorated functions to retrieve the config file. That makes sense to avoid I/O getting the env file.

config.py  from pydantic import BaseSettings      class Settings(BaseSettings):      app_name: str = "Awesome API"      admin_email: str      items_per_user: int = 50        class Config:          env_file = ".env"    

and then in other modules, the documentation implemented a function that gets the settings

#module_omega.py  from . import config    @lru_cache()  def get_settings():      return config.Settings()    settings = get_settings()  print(settings.ENV_VAR_ONE)  

I am wondering if this method is better practice or advantageous to just initializing a settings object in the config module and then importing it like below.

#config.py  from pydantic import BaseSettings      class Settings(BaseSettings):      app_name: str = "Awesome API"      admin_email: str      items_per_user: int = 50        class Config:          env_file = ".env"    settings = Settings()    
#module_omega.py    from .config import settings    print(settings.ENV_VAR_ONE)  
https://stackoverflow.com/questions/66683480/why-does-documentation-use-lru-cache-decorated-function-to-get-settings-instead March 18, 2021 at 09:19AM

没有评论:

发表评论