I'm trying to understand the differences in functionality and purpose between g
and session
. Both are objects to 'hang' session data on, am I right? If so, what exactly are the differences and which one should I use in what cases?
1 Answer
No, g
is not an object to hang session data on. g
data is not persisted between requests.
session
gives you a place to store data per specific browser. As a user of your Flask app, using a specific browser, returns for more requests, the session data is carried over across those requests.
g
on the other hand is data shared between different parts of your code base within one request cycle. g
can be set up during before_request
hooks, is still available during the teardown_request
phase and once the request is done and sent out to the client, g
is cleared.
-
18So
session
is 'per client' data (based on a cookie?) andg
is per-request data? Commented Oct 2, 2015 at 15:24 -
19@AvivCohn: exactly. The advantage of
g
is that it exists across all of your request, is thread safe, and specific to your current app (if you were to nest Flask apps where one calls another this becomes important). So you can set a user object or database connection with abefore_request
hook, then access that same user or connection in your templates, and still have available in a teardown hook without having to pass it along to each call. Commented Oct 2, 2015 at 15:25