7

I would like to set a global variable and use it as a trigger of various functions. Each user has a separate global variable. This is used to keep track of previous message data and proceed a conversation. The problem is that how can I manage a separate global variable to each user? The app is running once I deployed it in the server. When I attempt to change the global variable, this variable applies to every user, not just that single user who triggered its the change.

I'm using python flask without a DB.

Thank you.

Julianna Lee
  • 93
  • 1
  • 1
  • 4

2 Answers2

3

You don't need a global variable for this and truly speaking you won't need them anytime in the future as it is a bad practice to use global variables. Refer to this link for details, why are global variables evil?

Now coming to your problem, you probably need g module of flask which creates a context which persist over multiple requests from the same user. You can do something like this:

from flask import g

...

def get_messages():
    messages = getattr(g, '_messages', None)
    if messages is None:
        g._messages = []  # to store messages you may use a dictionary

    return g._messages

def add_message(message):
    messages = get_messages()
    messages.append(message)
    setattr(g, '_messages', messages)
    return messages

And remember for each user a different thread of the app is created so the neither the variables are shared nor their values. So for each user there will be a different g but it will persist over multiple requests from the same user. Hope it helps!

Edit: g object is a proxy to application context and doesn't persists across multiple requests. It persists for a single requests across the application.

Animesh Kumar
  • 162
  • 3
  • 5
  • 3
    You have written: "you probably need g module of flask which creates a context which persist over multiple requests from the same user". However, g is per request and not per user. It will not persist per user. It will persist only within a request. – variable Nov 01 '19 at 12:44
  • You have written that global variables are bad practice, and OP should use Flask's `g`, but Flask's `g` is a global variable! – Seweryn Niemiec Apr 12 '20 at 12:21
  • @SewerynNiemiec `g` is not a global variable, it's a proxy to the application context. It persists across the application context for a request. Though, it does not persists across the requests. – Animesh Kumar Apr 14 '20 at 12:23
  • @AnimeshKumar yes, but it acts exactly as a global variable. The application context is a special kind of global storage, it has the same function as global variable - it allows you to store values from any place and get them from any place, so it allows you to write same spaghetti code as using ordinary global variables. I'm not saying that global variables are always bad, but if you say that OP doesn't need globals then don't propose `g`. – Seweryn Niemiec Apr 15 '20 at 16:10
  • @SewerynNiemiec Gobal variables are almost always bad but using `g` is not. It is a module provided by flask itself for the purpose of storing value from anywhere and getting from anywhere. It would not result in spaghetti code as whenever you would encounter `g`, you would realize it is a flask module for storing global data and you would check it's other uses too. – Animesh Kumar Apr 21 '20 at 15:33
  • this code doesn't work, since `g` requires app context to work..... – avocado Feb 01 '22 at 19:10
2

Not sure if I'm understanding your question correctly, but perhaps you can create a global dictionary with key as the username and value as the message.

So each time you want to update the variable in the dictionary you can access the respective variable based on the username (key of the dictionary)

userMsg = {"John":"hello"} #existing user
# update John's msg
userMsg["John"] = "bye"
# now userMsg is {"John":"bye"}

# add new user if user does not exist
userMsg["Mary"] = "Your Message Here"
# now userMsg is {"John":"bye", "Mary":"Your Message Here"}
chowsai
  • 565
  • 3
  • 15
  • I am wondering what are the risks of storing in a dict compared to do it in the flask.g proxy ? – Tirbo06 Feb 01 '23 at 12:47