Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask tutorial: Why do we use the app context for the DB connection?

I'm studying the Flask tutorial and am confused about the usage of the app context for connecting to the database. On the page, the author says:

Creating and closing database connections all the time is very inefficient, so you will need to keep it around for longer. Because database connections encapsulate a transaction, you will need to make sure that only one request at a time uses the connection.

However, it seems that creating and closing the connection is exactly what is accomplished by the code. We have a view get_db that connects to the database, returns a connection object, and adds it to the app context if not present. We also have a close_db view that closes the connection object when the app context is torn down.

My understanding of what's happening here is as follows: A user connects to the app's home page, establishing a connection with the database and then closing it. Each time the user makes a request to the page (e.g., posting a new blog entry), a new connection is established with the database and then closed when the request is completed. So what's the value of using the app context at all? Why not just directly create and close a connection for each request?

Maybe I am misunderstanding something here -- any insight appreciated!

like image 449
Matthew Chatham Avatar asked Aug 02 '17 21:08

Matthew Chatham


People also ask

What is the application context in Flask?

The application context keeps track of the application-level data during a request, CLI command, or other activity. Rather than passing the application around to each function, the current_app and g proxies are accessed instead.

How can we request database connection in Flask?

To preserve the database connection within the same flask session you may use the application context and assign database connection. If the connection breaks,the context will try to establish the connection back as it polls the connection object continuously.

What is app context?

Application Context: It is the application and we are present in Application. For example - MyApplication(which extends Application class). It is an instance of MyApplication only. Activity Context: It is the activity and we are present in Activity. For example - MainActivity.


1 Answers

It's badly documented. But basically the whole point of the app context is to abstract app-specific-but-persistent-across-request data objects (ostensibly to allow multi-tenant applications that are all separate instances of Flask without tying everything to request context). In any case, flask.g is the app context-specific dict for the current app context. This way you can call get_db() on the very first request, shove the resulting connection object into g and then use g.dbconn later on in the app, which doesn't get killed on each request (presumably you want to use the same connection on each page access). I like to think of flask.g is like a class (well the app, in this case) instance of globals().

See also: http://flask.pocoo.org/docs/0.12/appcontext/#app-context

like image 69
cowbert Avatar answered Oct 04 '22 15:10

cowbert