Forums

How do I load global variables which are accessed by my apis in my Flask app on pythonanywhere

Hello! I would like to run the code below after starting my Flask app and before any of the endpoints are called. The variables model, data and qa_pairs are used across the endpoints of my flask app. After looking at this forum, I decided to run this code in the wsgi file as we don't execute anything inside the name = main clause.

However when I did this, I found that sometimes my endpoints will work and sometimes it will not. For example, out of 5 requests, 1 would be successful. I think this is because these global variables are sometimes initialized and sometimes not. Could this be because of the multiple worker process? If so, how would I go about fixing this or is there a better way to load these global variables.

Thanks in advance for any help.

model_id = "sentence-transformers/all-MiniLM-L6-v2"
model = SentenceTransformer(model_id)
data = {} 
qa_pairs = {}
def set_up_answers():
  with open('KnowledgeBaseReplicaFINAL.csv', 'r', encoding='latin-1') as file:
    reader = csv.reader(file)
    # skip the header
    next(reader)
    for row in reader:
        qna_id = int(row[8])
        question = row[0]
        long_answer = row[1]
        short_answer = row[2]
        context_needed = False if row[9] =='FALSE' else True
        in_course = False if row[10] =='FALSE' else True
        prompts_list = []
       .....
       data[qna_id] = {'Question':question, ....}

  for qnaid in data:
      info = data[qnaid]
    if not info['ContextNeeded']:
        qa_pairs[info['Question']] = qnaid
 print('finished setting up answers')

That's correct, your global variables would not be shared between the processes. Maybe you could store the data in a database instead?