为什么 session._get_current_object() 返回的对象的 id 在每次请求时都会改变,而 session 的 id 保持不变? [英] Why is id of object returned by session._get_current_object() is changed on every request, while id of session remains unchanged?

查看:28
本文介绍了为什么 session._get_current_object() 返回的对象的 id 在每次请求时都会改变,而 session 的 id 保持不变?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用:

  • Python 3.6.1
  • 烧瓶 0.12.2

关于session的部分的 Flask 文档说:

Section on session of Flask documentation says that:

这是一个代理.

关于代理的部分详细说明:

Flask 提供的一些对象是其他对象的代理.这背后的原因是这些代理之间共享线程,它们必须分派到绑定到一个的实际对象根据需要在幕后线程.
...
如果您需要访问到被代理的底层对象,您可以使用_get_current_object() 方法

Some of the objects provided by Flask are proxies to other objects. The reason behind this is that these proxies are shared between threads and they have to dispatch to the actual object bound to a thread behind the scenes as necessary.
...
If you need to get access to the underlying object that is proxied, you can use the _get_current_object() method

这一切都非常简单.
但是当我尝试以下操作时:

This all is pretty much straightforward.
But when I try the following:

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('________________________________')

    return 'Check the console! ;-)'

每次我向/发出请求——id(session._get_current_object())的值不同,而id(session)代码>保持不变.

each time I make a request to / — the value of id(session._get_current_object()) is different, while id(session) remains the same.

按照上面引用的 Flask 文档,应该是相反的.那么为什么会发生这种情况?

Following Flask documentation, quoted above, it should be the other way around. So why is this happening?

更新
受到 brunns 在对 他的回答,每个线程有一个底层对象

UPDATE
inspired by brunns's suggestion in the comments to his answer, that there is one underlying object per thread

这是一些代码,用于测试每个线程有一个底层 session 对象 (session._get_current_object()) 的假设:

Here is some code, to test assumption that there is one underlying session object (session._get_current_object()) per thread:

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! ;-)'

尽管有预期,threading.current_thread().ident) 从未改变,而 id(session._get_current_object() 正在改变.

Despite the expectations, threading.current_thread().ident) is never changed, while is id(session._get_current_object() is changing.

推荐答案

session 是您从 flask 模块导入的对象.你只导入一次,它不会改变,它的 id() 也不会改变.它在线程之间共享,并且是底层对象的代理.

session is an object you have imported from the flask module. You only import it once, and it doesn't change, so nor will its id(). It's shared between threads, and it's a proxy to the underlying objects.

每个请求可能运行在不同的线程上,每个请求都有不同的底层对象,所以它们可能有不同的id()s.

Each request may be run on a different thread, and each will have a different underlying object, so they may have different id()s.

这篇关于为什么 session._get_current_object() 返回的对象的 id 在每次请求时都会改变,而 session 的 id 保持不变?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆