为什么每次会话都更改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?

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

问题描述

我正在使用:

  • Python 3.6.1
  • 烧瓶0.12.2

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

Section on session of Flask documentation says that:

这是代理.

This is a proxy.

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

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.

在上面引用的瓶文档之后,应该采用另一种方法.那为什么会这样呢?

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().

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天全站免登陆