Both current_app
and g
are application context variables, so they are loaded and unloaded with each request, so anything data stored on them will only be available within the same request.
The only difference I can see is that g
starts empty at the beginning of each request, while current_app
starts with some attributes (like config
) that are copied from the application object. But that wouldn't justify having g
object at all, since one could just as easily store new information on current_app
.
What's the difference that I don't see?
app_context(). If you see that error while configuring your application, such as when initializing an extension, you can push a context manually since you have direct access to the app . Use app_context() in a with block, and everything that runs in the block will have access to current_app .
g is an object for storing data during the application context of a running Flask web app. g can also be imported directly from the flask module instead of flask. globals , so you will often see that shortcut in example code.
Purpose of the Context Flask uses the term context local for this. Flask automatically pushes a request context when handling a request. View functions, error handlers, and other functions that run during a request will have access to the request proxy, which points to the request object for the current request.
You should also be able to fix the following error: RuntimeError: Working outside of application context. This typically means that you attempted to use functionality that needed to interface with the current application object in some way. To solve this, set up an application context with app.
I believe that you read the docs on g
and on current_app
. So what i could understand from it:
current_app
:
Points to the application handling the request. This is useful for extensions that want to support multiple applications running side by side.
[emphasis mine]
So you are getting context of current app, while g
stores everything, from source code:
def _lookup_app_object(name): top = _app_ctx_stack.top if top is None: raise RuntimeError(_app_ctx_err_msg) return getattr(top, name) def _find_app(): top = _app_ctx_stack.top if top is None: raise RuntimeError(_app_ctx_err_msg) return top.app current_app = LocalProxy(_find_app) g = LocalProxy(partial(_lookup_app_object, 'g'))
So if you're running several applications, current_app
would reference to current one(obvious, right), and g
to everything.
From the documentation:
When the request starts, a RequestContext is created and pushed, which creates and pushes an AppContext first if a context for that application is not already the top context. While these contexts are pushed, the current_app, g, request, and session proxies are available to the original thread handling the request.
When you are on a browser and made your first request, the and application context is created and pushed out to the stack and then on the top of the stack, the request context are created and popped out. The point is - the application context is at the bottom of the request stack data structure.
More so on g: It's a namespace object that can store data during an application context. This is a good place to store resources during a request. g is a special object that is unique for each request. It is used to store data that might be accessed by multiple functions during the request.
The connection is stored and reused instead of creating a new connection if get_db is called a second time in the same request.
more: When should Flask.g be used?
Side note: Checkout the user for Context Processors (The dict returned by that, is available directly to all the templates )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With