I’m using Dash to create apps on-the-fly using an API I wrote and I’ve got it all working fine, except for when I serve it using gunicorn
with multiple workers.
My API creates a dash.Dash()
object and loads it into an instance of werkzeug.wsgi.DispatcherMiddleware.mounts
so that it is served on a new URL.
Unfortunately, each workers has its own server, so the wsgi_app.mounts
dictionary is only updated for that one worker that handled the API request. The other ones don’t know about the new dash.Dash()
app or from what URL it should be accessed.
My thought was that I’d fix this by modifying DispatcherMiddleware
to read it’s mounts
object from Redis and just keep a master version in Redis, updating it whenever a new dash.Dash()
is created and reading from it each time a worker tries to serve something.
Unfortunately, I’m having trouble serializing dash.Dash()
objects with dill
if they include any of the objects from dash_html_components
or dash_core_components
, which obviously they do:
_pickle.PicklingError: Can't pickle <class 'Div'>: it's not found as builtins.Div
Also, PyCharm gives me an inspection error on all these classes:
Cannot find reference to 'Div' in __init__.py
I don’t know if those issues are related, but it seems suspicious to me.
Here’s a quick code to reproduce this:
from dash import Dash
import dash_html_components as html
from dill import dumps
dash = Dash(__name__)
dash.layout = html.Div('Dash App')
serialized = dumps(dash)
Anyone know how I might go about storing these objects? Is there some other way to make all the workers in gunicorn
aware of the changes?