Looking for help using global data in a Flask application across sessions by different users Looking for help using global data in a Flask application across sessions by different users flask flask

Looking for help using global data in a Flask application across sessions by different users


I'm not sure that trying to cache a blob of data that is preserved globally at the application level is possible using vanilla Flask. Additionally, even if you could do it, I'm not so sure that it's a good idea.

Here's why I think that it shouldn't be done: When you have each request stored in its own "sandbox," you know that when a request is made, it's going to access data in that request's sandbox. But if you make the "sandbox" global and accessible to everyone then there's no way to really control access to the sandbox. You could easily end up running into concurrent access problems. Then you'd have to design a queuing process to make sure there is only one request accessing the global sandbox at a time. You may have to extend the builtin objects to include locks (once again, having to do with data security/integrity in a world of multiple access).

Also keep in mind that Flask global g is not the way to get around this problem. Flask g is sort of a misnomer. It's not actually applied to the global application context. In other words, it doesn't live at a level where all users who are accessing the application also have access to g. Rather, g is rewritten at the request level. In other words, it's still accessed (and can be overwritten/modified) at the request level. Check out this SO post for more information.


EDIT: If you really need it I think using a simple cache provided by werkzeug should suffice. Check the docs for usage.


You could put them in a configuration file. Using import my_config would bring the objects in for all users. Now the question is what can the users do with it. If they are expected to write to this data then this might not be the best idea.

So you declare variables in my_config.py

import numpy as npmy_var = np.arange(24).reshape(6,4)

Then you use them in your flask app

import flaskfrom my_config import my_varmore flask app here...