Dalam aplikasi saya, status objek umum diubah dengan membuat permintaan, dan responsnya bergantung pada status.
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 on my development server, I expect to get 1, 2, 3 and so on. If requests are made from 100 different clients simultaneously, can something go wrong? The expected result would be that the 100 different clients each see a unique number from 1 to 100. 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.
Since there were only two clients, the expected results were 1 and 2, not 2 and 3. A number was skipped.
Will this actually happen as I scale up my application? What alternatives to a global variable should I look at?