Repository navigation
In what manner should shared database connections and HTTP clients be elegantly stored and accessed? #6851
QuestionI have a Flet app with many pages. Different users use separate database connections and httpx clients. These two objects need to be loaded when the program starts. I want to store and call them elegantly. Using shared global variables or class static variables causes conflicts between users. What approach can I use to initialize them gracefully and reference them from any page? Code sampleNo response Error messageNo response ------------------------------------------------------
|
Replies: 2 comments
|
Create the services object inside page.session.store.set("services", services)
# Later, within the same session:
services = page.session.store.get("services")Create separate service instances per session. Putting the same global object into two stores still shares it. The registry example in #5903 uses this pattern across modules. The store is transient and holds references, so connection cleanup and database/httpx concurrency still need their own handling. |
|
Use Singleton pattern in one file like db.py, initialize it and import in any file you like |
Create the services object inside
main(page)and pass it to your route/view constructors. Flet 1.0's session store also provides a lookup point for modules receiving the currentpage:Create separate service instances per session. Putting the same global object into two stores still shares it.
The registry example in #5903 uses this pattern across modules. The store is transient and holds references, so connection cleanup and database/httpx concurrency still need their own handling.