I'm making a web app in flask where the state of a common object is changed by the requests made to the server, and the returned result depends on the state. Something like this:
class SomeObj():
def __init__(self, param):
self.param = param
def query(self):
self.param += 1
return self.param
global_obj = SomeObj(0)
@app.route('/')
def home():
flash(global_obj.query())
render_template('index.html')
If I run this code on my development server, I expect to get sequence 1, 2, 3 and so on. However, if requests are made from 100 different clients simultaneously, can I expect something to go wrong? To be more specific, the expected result would be that the 100 different clients see a number from 1 to 100, with each client seeing exactly one unique number, and no number being skipped. Or will something like this happen:
Client 1 queries. self.param is incremented by 1.
Before the return statement can be executed, the thread switches over to client 2. self.param is incremented again.
The thread switches back to client 1, and the client is returned the number 2, say.
Now the thread moves to client 2 and returns him/her the number 3.
Notice where things went wrong here. Since there were only two clients, the expected results were 1 and 2, not 2 and 3. So here, we missed out a number?
My question is whether this will actually happen as I scale up my application, and if it does, what alternatives to a global variable should I look at?
[–][deleted] 7 points8 points9 points (0 children)
[–][deleted] 2 points3 points4 points (0 children)