I want to know whether a flask app running on uwsgi with multiple processes+threads can access a common memory data structure defined inside the Flask app class.
What I am trying to do: I have a flask app which will run on nginx + uwsgi. I want every http request to access some data which is lying in an etcd DB. However, this is slow. To speed up the http responses, I am thinking of loading the data from DB into the flask app's memory when the flask app starts. Therefore, on every request, this data can be quickly accessed from local memory instead of asking etcd.
How I am trying to do this: I have defined my own Flaskapp class and inherited it from "Flask" and have overridden "init" to load pre-appstart state:
class FlaskApp(Flask):
def __init__(self, *args, **kwargs):
self.populate_info() # This function will load stuff
super(FlaskApp, self).__init__(*args, **kwargs)
def populate_info(self):
self.stuff = []
#load stuff from db to self.stuff
Will every flask request be able to read self.stuff? Coming from a C++ background, as per my understanding, this depends on how uwsgi runs the flask app. So I need the answer to: 1) Does every uwsgi process instantiate the Flask App ? 2) Or does uwsgi instantiates the Flask App instance once in memory, shares it between every process, and then every process calls only the http request handlers of this instance on every request? I tried searching docs to understand how uwsgi schedules this, but could not find enough information to understand this. So will appreciate any help. If this memory sharing won't work(which is my hunch), what is an alternative way to do so? Thanks.
The threads option is used to tell uWSGI to start our application in prethreaded mode. That essentially means it is launching the application across multiple threads, making our four processes essentially eight processes.
After digging a bit deeper into the uwsgi docs, I found that there are 2 modes of function of uwsgi:
I found this link helpful: https://engineering.ticketea.com/uwsgi-preforking-lazy-apps/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With