I have two functions, A and B in flask:
@app.route("/")
def index():
session["var"] = 0
return render_template("index.html")
@app.route("/A")
def A():
session["var"] += 1
return {}
@app.route("/B")
def B():
i = 0
while(i < 300):
time.sleep(1)
print(session["var"])
i += 1
return {}
and in the client side, I call function A, wait for response, and then call function B without waiting for response.
A_response = await GET("A");
B_response = GET("B");
The problem is that if I do the operation above in the client side twice, the output in the server will be always 1 for the first call of B and 2 for the second call of B, what I want to achive is to get 2 as output for the two calls of B (ofcurse after I call both).
What I tried to do:
- set session as a global variable in the server side instead of importing it from Flask package. It worked locally for one process, but it didn't work in the cloud, also the problem got worse as function
Adidn't recognizesession["var"]. - I thought of using a database as it will solve the problem I described here, but I don't like this solution as I will have to deal with other problems that the flask.session has already solved for me, such as:
- It adds more complexity identifying a session between the server and the browser, which means, I need to recognize each user uniquly and get his information from the database in someway.
- I should somehow clear the database since now and then, its not so clear to me when should I do it, intuitively the session should be cleared from the database when the user closes the browser (which happens with Flask.session) but I don't know how exactly to implement that.
If there was a solution that can be made to Flask.session to solve the described problem, without replacing it with database that would be great!
- It adds more complexity identifying a session between the server and the browser, which means, I need to recognize each user uniquly and get his information from the database in someway.