4

I know there are frameworks that exist, but I am trying to use wsgi directly to improve my own understanding.

I have my wsgi handler, and up at the top I have declared a variable i = 0.

In my application(environ, start_response) function, I declare global i, then I increment i whenever a button is pressed.

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.

If I declare i inside the application function, then the value of i resets to 0 any time a request is made.

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.

And I know you could store i in a database between posts, but is it possible to just keep i in memory between posts?

1

2 Answers 2

3

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.

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.

Sign up to request clarification or add additional context in comments.

Comments

1

Add WSGI session middleware and store the value these.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.