Flask and its contexts

Posted on

Question :

I’m new to desenvolvimento Web and I’m studying FLask and it has some objects that should only be manipulated in certain context like Flask.g and Flask.current_app what I can not understand are the differences between these% with%. My doubts are:

  • What is contextos ?
  • What is contexto de solicitação ?
  • Are there other contexto de aplicação ?
  • What are the differences between contextos and Flask.g ?

Answer :

  • What is request context?
    The request context (or request ) appears when you apply to your application. At this time, according to documentation , a request context, along with an application context, is raised. This means that all the information about your request that has just arrived is available through the request object. This object is isolated from other requests and you do not need to know how many requests are being handled, you just have to worry about the one that has just arrived. This object holds information about your request, not the application. And how to know about my application data, like the settings I just set? The answer is in the next question …

  • What is application context?
    In the context of the application you have access to the settings of your application, routes and etc. This context, as I have already said, is available along with the context of the request at the time it is made. This means that both have almost the same life cycle, the context of the request being dropped before the application context. Therefore, the request context ends first (this information may be useful depending on what you want to do).
    But why is there an application context? Well, that depends on what you’re doing. You can work with multiple instances of your application and each will react in a way to a particular request, for example. With the contextual information of your application you can perform tasks in one that the other would not perform. This is an example. There are certainly elegant ways to use this feature that does not fit here and is not the focus of my answers, but it’s worth going deeper.

  • Are there other contexts?
    I use multicontext in applications in the company where I work. Each request carries a header indicating which context it wishes to use. Within my application a treatment is done for the context to change the way I want, changing for example the database that the application points to, or even the server that it communicates. The flask is beautiful so you can do fantastic things without getting stuck to a certain pattern or way of doing.

  • What are the differences between Flask.g and Flask.current_app ?
    This is a more technical question and you respond by referring to this information here and here . But the crucial difference is that g is a globally accessible variable at request time. It is basically used to store information that you later want to retrieve globally in your project (later, however, STILL at the time of the request). current_app stores your application information, such as settings and routes, for example. It’s basically an app copy of Flask.

I hope I have helped with the answers. Good studies!


What are the differences between Flask.g and Flask.current_app?

There was a change in Flask 0.10. Flask.g exists within the application context. The application context is available during the time of each request. Flask.current_app represents the application context.


Leave a Reply

Your email address will not be published. Required fields are marked *