I am playing around with Flask, striving to understand details of how sessions are working, I am using:
- Python 3.6.1
- Flask 0.12.2
Flask documentation clearly states (bold is mine):
The session object works pretty much like an ordinary dict, with the difference that it keeps track on modifications.
This is a proxy.
...
Section on proxies mentions that (again, bold is mine):
If you need to get access to the underlying object that is proxied, you can use the
_get_current_object()
method
Thus, underlying object (session._get_current_object()
) must remain the same for the request or, as was suggested by this answer and comment, a thread. Though, it does not persist nor inside request, nor thread.
Here is a demonstration code:
import threading
from flask import (
Flask,
session,
)
app = Flask(__name__)
app.secret_key = 'some random secret key'
@app.route('/')
def index():
print("session ID is: {}".format(id(session)))
print("session._get_current_object() ID is: {}".format(id(session._get_current_object())))
print("threading.current_thread().ident is: {}".format(threading.current_thread().ident))
print('________________________________')
return 'Check the console! ;-)'
If you will run Flask application above, and repeatedly go to the /
— id returned by session._get_current_object()
will, occasionally, change, while threading.current_thread().ident
never changes.
This leads me to ask the following questions:
- What exactly is returned by
session._get_current_object()
? - I get that it is an object underlying
session
proxy, but what this underlying object is bound to (if it is not a request and not a thread, if anything I would expect it never to change, for the simple application above)?