r/FastAPI • • May 12 '25
Question Lifespan for loading configuration
I'm looking to load some configuration settings from a YAML file. The path to the YAML file is provided via the command line. Once loaded, I need these configurations to be accessible throughout the entire application, including in services, routers, and other components.
I'm wondering what the best approach would be to achieve this. Would using a context manager with a lifespan (such as in FastAPI's lifespan event) be a good solution for this use case?
9
Upvotes
View all comments
Show parent comment
1
u/BluesFiend May 12 '25
Global args will have unintended side effects one day and catch you out, they are generally best avoided and they complicate imports, introduce more risk of circular imports etc.
If it works it works, but a dependency isn't going to cause random unintended issues.