Prevent code to be re-executed with auto-reload enabled #58
Unanswered
AndreuCodina
asked this question in
Questions
Replies: 1 comment
-
If that line is not re-executed, then your singleton won't be initialized at all. If you can test and develop some functions without that singleton, I suggest changing your class to not execute the costly code until the first access of azure services is needed. You could also control this by having separate dev and production development environments where developers do not need to go to azure every time they save a code change. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
First Check
Commit to Help
Example Code
Description
We read the configurations and secrets from Azure Key Vault (https://github.com/pydantic/pydantic-settings/blob/main/docs/index.md#azure-key-vault) and Azure App Configurations (https://github.com/AndreuCodina/pydantic-settings-azure-app-configuration) when FastAPI is executed with auto-reload and we store them in a
BaseSettings
class (from thepydantic-settings
package). In summary, we instantiate a singleton of that class.When a piece of code is changed, the auto-reload executes
main.py
again and the singleton is re-instantiated. Each reading costs money and takes time, so we need to not re-execute the singleton instantiation when FastAPI detects a change.Operating System
Linux, Windows, macOS
Operating System Details
Latest.
Python Version
3.12.4
Additional Context
No response
Beta Was this translation helpful? Give feedback.
All reactions