
When I create a new vercel deployment with flask, it runs the main build function several times in a row. app.route("/"). I don't know why that happens.
@app.route('/')def build(): return render_template('chatdoc.html')Also, I'm trying to maintain a specific session id between requests from different functions from my server. Inside of the build function, I first create a new session id to identify each unique user on my server and use that id as a key value to store a specific user's queries.
user_queries = {}
@app.route('/')def build(): session_id = session.get("session_id") if not session_id: session_id = str(uuid.uuid4()) session['session_id'] = session_id
if session_id not in user_queries: user_queries[session_id] = [] return render_template('page.html')But somehow, vercel runs this function multiple times and creates multiple session_ids and hence multiple keys inside of user_queries. When I call a function to get the data, it works fine at first and the very first session_id created is what is remembered. However, when I switch to a different page and get the session_id from there, it returns the very first session_id which was created, sometimes with an empty dictionary from the user_queries.
@app.route('/home')def home(): session_id = session.get('session_id') print(session_id) print(user_queries) return render_template('home.html')In the home function, when I reload the page multiple times, the session_id returns None the user_queries are also forgotten and returns an empty dictionary despite being a globa variable.
when I use a different function to get the session_id as well, it also returns the very first one, but this time the dictionary is empty despite being a global variable. When I reload the newly visited homepage again, this time, it returns None as the session_id and an empty dictionary for user_queries.
@app.route('/respond', methods=['POST'])def respond(): session_id = session.get("session_id") print(user_queries[session_id]) return user_queries[session_id]In some cases, after I reload the homepage, when this respond function is called, it returns an empty dictionary, sometimes a previously created one, and also returns the latest session_id when the session.get function is called. (the latest session_id, created in the app.route("/") function.) Then I get a 'KeyError' message.
Why does all of this happen. Can someone please tell me how to fix it?