I want to provide shared state for a Flask app which runs with multiple workers, i. e. multiple processes.
To quote this answer from a similar question on this topic:
You can’t use global variables to hold this sort of data. […] Use a data source outside of Flask to hold global data. A database, memcached, or redis are all appropriate separate storage areas, depending on your needs.
(Source: Are global variables thread safe in flask? How do I share data between requests?)
My question is on that last part regarding suggestions on how to provide the data “outside” of Flask. Currently, my web app is really small and I’d like to avoid requirements or dependencies on other programs. What options do I have if I don’t want to run Redis or anything else in the background but provide everything with the Python code of the web app?
2
Answers
If your webserver's worker type is compatible with the
multiprocessing
module, you can usemultiprocessing.managers.BaseManager
to provide a shared state for Python objects. A simple wrapper could look like this:You can assign your data to the
shared_dict
to make it accessible across processes:However, you should be aware of the following circumstances:
shared_lock
to protect against race conditions when overwriting values inshared_dict
. (See Flask example below.)BaseManager
process dies, the shared state is gone.BaseManager
, you cannot directly edit nested values inshared_dict
. For example,shared_dict["array"][1] = 0
has no effect. You will have to edit a copy and then reassign it to the dictionary key.Flask example:
The following Flask app uses a global variable to store a counter number:
This works when using only 1 worker
gunicorn -w 1 server:app
. When using multiple workersgunicorn -w 4 server:app
it becomes apparent thatnumber
is not a shared state but individual for each worker process.Instead, with
shared_dict
, the app looks like this:This works with any number of workers, like
gunicorn -w 4 server:app
.your example is a bit magic for me! I’d suggest reusing the magic already in the
multiprocessing
codebase in the form of aNamespace
. I’ve attempted to make the following code compatible withspawn
servers (i.e. MS Windows) but I only have access to Linux machines, so can’t test therestart by pulling in dependencies and defining our custom
Manager
and registering a method to get out aNamespace
singleton:this might need to be more complicated if creating the initial state is expensive and hence should only be done when it’s needed. note that the OPs version of initialising state during process startup will cause everything to reset if gunicorn starts a new worker process later, e.g. after killing one due to a timeout
next I define a function to get access to this shared state, similar to how the OP does it:
though I’m not sure if I’d recommend doing things like this. when
gunicorn
starts it spawns lots of processes that all race to run this code and it wouldn’t surprise me if this could go wrong sometimes. also if it happens to kill off the server process (because of e.g. a timeout) every other process will start to failthat said, if we wanted to use this we would do something like:
this uses Unix domain sockets (passing a string rather than a tuple as an address) to lock this down a bit more.
also note this has the same race conditions as the OP’s code: incrementing a number will cause the value to be transferred to the worker’s process, which is then incremented, and sent back to the server. I’m not sure what the
_lock
is supposed to be protecting, but I don’t think it’ll do much