Sharing Memory in Gunicorn? Sharing Memory in Gunicorn? flask flask

Sharing Memory in Gunicorn?


It looks like the easiest way to do this is to tell gunicorn to preload your application using the preload_app option. This assumes that you can load the data structure as a module-level variable:

from flask import Flaskfrom your.application import CustomDataStructureCUSTOM_DATA_STRUCTURE = CustomDataStructure('/data/lives/here')# @app.routes, etc.

Alternatively, you could use a memory-mapped file (if you can wrap the shared memory with your custom data structure), gevent with gunicorn to ensure that you're only using one process, or the multi-processing module to spin up your own data-structure server which you connect to using IPC.