-
-
Notifications
You must be signed in to change notification settings - Fork 948
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
What is the best way to store globally accessible "heavy" objects? #374
Comments
Depends on if you're asking about "during app's lifetime" or "during the request/response cycle". (Flask's For stuff that has a request/response lifespan, you can store arbitrary stuff on the request "scope". For lifespan, use globals, with |
Happy to dig further into any specific questions around this too. (And consider if we end up wanting any helpers for task-local context) |
Makes sense! Yeah, it seems that I've merged two different questions into a single one :) |
everyone loves tom |
(Asking this here for the benefit of the community) How would one achieve the "during the request/response cycle" variant, in a middleware? We have a weird configuration where our calling client POST's a weird json body that we can't change. I flag this by using a custom qs parameter that signals to our app that it's one of these weird ones and needs to restructure it to something a normal person would recognize (a typical request with sane json format passes through unscathed). In a flask app, I use a route decorator function to check all this, then set this transformed value in something like It would be wonderful if I could modify the incoming request conditionally, in a middleware in Starlette. I haven't been able to locate the ideal way to do this, and |
@chris-erickson Okay - a hack for this right now would be accessing the private I think we should probably expose a public interface for storing per-request information on the ASGI scope. I'll open a ticket for that. |
Great, that works as I would expect, and should suffice for now. A per-function decorator was something else I considered based on something else I saw. Would you say that way is not preferred? It's slightly more "obvious" in that you know exactly which routes will get this behavior but in my case, it's not really important. |
That'd work absolutely fine too, yup. Just depends if it's beahvior that you want to apply to all incoming requests, or to particular endpoints. |
How do we do this? I've tried to set a global variable outside of any functions in my |
@ohmeow I know it's a little late, but the solution is to write
within the function. |
how would I use that
but I get: Do I need to import |
I believe I got it working using 'settings'. settings.py
main.py
another_file.py
edit: or better, set the variable via a function in the same module:
main.py
routes/another_file.py
|
In Flask we have a
g
global context to keep the global variables of the request. A kind of canonical example from the docs:So I wonder if there is something similar in
starlette
? I have a couple of very weighty objects which I don't want to initialize too often. These objects are "read-only" and are not modified during app's lifetime. What is the best way to handle cases like this?The text was updated successfully, but these errors were encountered: