Flask and its contexts

Asked

Viewed 154 times

3

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

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

2 answers

1


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

  • What is context of application? In the context of the application you have access to your application settings, routes, etc. This context, as I said, is available along with the context of the request at the time it is made. That means they both have almost the same life cycle, the context of the request being dropped before the context of the application. Therefore, the context of the request 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 several instances of your application and each one will react in a way to a certain request, for example. With the context information of your application you can perform tasks in one that the other would not perform. This is an example. Surely there are elegant ways to use this resource that does not fit here and is not the focus of my answers, but it is worth deepening the subject.

  • There are other contexts? I use multicontexto in applications in the company where I work. Each request brings with it a header pointing out what context it wants to use. Within my application a treatment is carried out so that the context changes in the way I want, changing for example the database that the application points, or even the server that it communicates. 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? That’s a more technical question and you answer it by consulting this information here and here. But the crucial difference is that g is a global variable accessible at request time. It is basically used to store information that you want to retrieve later globally in your project (later, but STILL at the time of the request). current_app stores your application information, such as settings and routes, for example. It’s basically a copy of Flask’s app.

I hope I helped with the answers. Good studies!

  • 1

    Thank you for the reply.

1

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

There has been 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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.