问题描述
我知道有存在的框架,但我试图用WSGI直接提高我自己的理解。
I know there are frameworks that exist, but I am trying to use wsgi directly to improve my own understanding.
我有我的WSGI处理程序,并在最顶端我已经声明的变量 I = 0
。
I have my wsgi handler, and up at the top I have declared a variable i = 0
.
在我的应用程序(ENVIRON,start_response)
功能,我宣布全局I
,然后我增加 I
每当一个按钮为pressed。
In my application(environ, start_response)
function, I declare global i
, then I increment i
whenever a button is pressed.
这是我的理解,这个变量的状态为preserved只要服务器在运行,所以Web应用程序的所有用户看到相同的 I
It is my understanding that the state of this variable is preserved as long as the server is running, so all users of the web app see the same i
.
如果我宣布 I
的应用
函数内部,那么的值I
复位为0的请求作出的任何时间。
If I declare i
inside the application
function, then the value of i
resets to 0 any time a request is made.
我想知道,你怎么了preserve I
单个用户的请求之间,但又不希望它$不同用户的会话pserved p $?因此,一个用户可以让多个帖子,而 I
将增加,但如果另一个用户访问Web应用程序,他们开始与 I = 0
。
I'm wondering, how do you preserve i
between a single user's requests, but not have it preserved across different users' sessions? So, a single user can make multiple posts, and i
will increment, but if another user visits the web app, they start with i=0
.
我知道你能 I
存储在岗位之间的数据库,但有可能只保留 I
在岗位之间的记忆?
And I know you could store i
in a database between posts, but is it possible to just keep i
in memory between posts?
推荐答案
Web的应用程序一般都是无共享。在WSGI的背景下,这意味着你不知道有多少次你的应用程序(和它的计数器)将被实例化;这样的选择是到它作为您的应用程序的容器WSGI服务器。
Web-apps are generally "shared-nothing". In the context of WSGI, that means you have no idea how many times your application (and the counter with it) will be instantiated; that choice is up to the WSGI server which acts as your app's container.
如果您希望用户会话的一些概念,你必须明确地实现它,通常在饼干上。如果你要坚持,你需要明确地支持它的组成部分;这可以是一个共享的数据库,也可以背驮式Cookie的会话。
If you want some notion of user sessions, you have to implement it explicitly, typically on top of cookies. If you want persistence, you need a component that explicitly supports it; that can be a shared database, or it can piggy-back on your cookie sessions.
这篇关于在Python和Apache mod_wsgi的全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!