9

I am trying to find out how to work with global variables in Flask:

gl = {'name': 'Default'}

@app.route('/store/<name>')
def store_var(name=None):
    gl['name'] = name
    return "Storing " + gl['name']

@app.route("/retrieve")
def retrieve_var():
    n = gl['name']
    return "Retrieved: " + n

Storing the name and retrieving it from another client works fine. However, this doesn't feel right: a simple global dictionary where any session pretty much simultaneously can throw in complex objects, does that really work without any dire consequences?

1
  • 3
    Checkout Redis it provides distributed thread safe data structures for just this purpose. Keep in mind that this will also fail if you are running many load balanced python processes. If a request gets directed to a different process its going to have different values stored in its local dictionary.
    – nsfyn55
    May 5, 2014 at 15:54

1 Answer 1

17

No, it doesn't work, not outside the simple Flask development server.

WSGI servers scale in two ways; by using threads or by forking the process. A global dictionary is not a thread-safe storage, and when using multi-processing changes to globals are not going to be shared. If you run this on a PAAS provider like Google App Server, the processes aren't even forked; they are run on entirely separate machines even.

Use some kind of backend storage instead; a memcached server, a database server, something to control concurrent access and share the data across processes.

9
  • If the gl was declared in a module 'foo.py' which is then imported in the above code using 'import foo', and then if user used foo.gl to set and get the value, then would it be good solution?
    – variable
    Nov 1, 2019 at 9:29
  • @variable: no, that's no different from using the same dictionary from within the foo module. Importing a dictionary from another module doesn't change the nature of the dictionary or protect you from concurrency issues.
    – Martijn Pieters
    Nov 1, 2019 at 10:49
  • Ok. For each request, for example (/retrieve), does the function (retrieve_var) run in a new stack frame in a new thread? So when the thread runs, the OP's code is setting 'name' key to value 'default'. Hence the /retrieve will always return 'Default' - am I right?
    – variable
    Nov 1, 2019 at 10:57
  • Or does the new thread only run the function and still has access to globals modified by previous url requests (although not thread safe)?
    – variable
    Nov 1, 2019 at 11:04
  • @variable: this is getting to be quite broad and a general concurrency discussion. 'stack frames' have nothing to do with concurrency really, stack frames are created whenever you call a Python function, regardless of threading. Within a single process all threads have access to the same globals. If requests are being handled by multiple processes (on a single machine or on multiple), then different processes have different globals.
    – Martijn Pieters
    Nov 1, 2019 at 11:09

Not the answer you're looking for? Browse other questions tagged or ask your own question.