flask会话变量是否保持跨线程状态? [英] Does flask session variable maintain state across threads?
问题描述
根据文档:
一个会话基本上可以记住从一个
请求到另一个的信息。
但在其他地方,文档说 a> 我只看到两种选择: 我缺少什么东西? 更新:根据@Daniel Roseman的回答,我最后的猜测是正确的,除了瓶子本身不做,而是要求用户代理以存储/检索持久状态(因此,状态不是每个瓶子进程或每个瓶子应用程序),而是根据用户代理发生持续状态的任何集合(可能称为用户会话)。 Flask这样做的方式是使用签名的cookie。 在发送响应之前,Flask将信息存储在cookie中,并在下一个请求开始时将其读回。 According to the docs: A session basically makes it possible to remember information from one
request to another. But elsewhere, the docs say that I only see two alternatives: Am I missing something? Edit: I guess another alternative is: Update: based on @Daniel Roseman answer, my last guess was correct, except flask doesn't do it itself but rather asks the user agent to store / retrieve persistent state (and thus, the state is not per flask process or per flask application but rather per whatever collection of requests the user agent happens to persist the state over - what one might call user session). From the very next sentence after the one you quoted: The way Flask does this is by using a signed cookie. Flask stores the information in the cookie before sending the response, and reads it back at the beginning of the next request. 这篇关于flask会话变量是否保持跨线程状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! flask
(比如 app.run(threaded = True)
),怎么能它履行了这个承诺?
$ ul
$ 以某种方式确保相同的用户会话始终由相同的线程提供服务(这看起来很可怕,因为在两个选项卡中浏览的单个用户在处理第二个请求之前必须等待第一个请求完成)如果我允许线程,
session
是完全没有用的,我需要将特定于会话的信息存储在数据库中并没有在文档中提及)
<编辑:我想另外一个选择是:
session
本身就是一个线程 - python透视下的本地变量(即python将每个线程的 session
看作是完全独立的对象),但是通过 flask
(大概是用一些进程全局内存数据结构)。在这种情况下,
flask
可以对 session
atomic进行所有修改(使用一些线程间同步机制) li>
session
is local to a thread. So if flask
is used in a multithreaded environment (say, app.run(threaded=True)
), how can it fulfill this promise?
flask
somehow ensures that the same user session is always serviced by the same thread (which seems horrible because a single user browsing in two tabs would have to wait for his first request to finish before his second request is handled) session
is completely useless if I allow threads, and I need to store session-specific information in a database (which seems rather unexpected and isn't mentioned in the docs)
session
itself is a thread-local variable from python perspective (i.e., python sees each thread's session
as completely independent objects), but it is somehow synchronized across threads by flask
(presumably with some process-global in-memory data structure). In that case, flask
could make all modifications to session
atomic (using some inter-thread synchronization mechanism).