python - Sharing Memory in Gunicorn? -
i have large read-only data structure (a graph loaded in networkx, though shouldn't important) use in web service. webservice built in flask , served through gunicorn. turns out every gunicorn worker spin up, worked holds own copy of data-structure. thus, ~700mb data structure manageable 1 worker turns pretty big memory hog when have 8 of them running. there way can share data structure between gunicorn processes don't have waste memory?
it looks easiest way tell gunicorn preload application using preload_app
option. assumes can load data structure module-level variable:
from flask import flask your.application import customdatastructure custom_data_structure = customdatastructure('/data/lives/here') # @app.routes, etc.
alternatively, use memory-mapped file (if can wrap shared memory custom data structure), gevent gunicorn ensure you're using 1 process, or the multi-processing module spin own data-structure server connect using ipc.
Comments
Post a Comment