Open fxstempfelals opened 7 months ago
Are you using the SQLAlchemy engine defined globally elsewhere? The multiprocess will try to transfer it when used but that is not valid pickle/multiprocess object. May need to establish the engine inside the callback for that to work or switch to celery.
The engine is not a global object but a property of an object created in the callback. Roughly:
class DBHandle:
def __init__(self, url, schema):
connect_args = {...}
self.engine = sqlalchemy.create_engine(
url,
echo=True,
pool_pre_ping=True,
connect_args=connect_args,
execution_options={"schema_translate_map": {None: schema}},
)
@dash.callback(
[...]
prevent_initial_call=True,
background=True,
manager=BACKGROUND_CALLBACK_MANAGER,
)
def _callback():
dbh = DBHandle(url, schema)
I'll try with Celery if I can't find a solution but it would be nice and make development easier if it could work with a local cache.
Could you try running your app with:
app.run(debug=True, dev_tools_prune_errors=False)
With that option the stacktrace in the screenshot should have more info to help identify which variable is causing the issue.
Thanks for the suggestion. After some digging, I realized the SSLContext
instance is a nested property of botocore.client.S3
. This S3 client is not used in the background callback that is the source of the error, but elsewhere in the app. I guess dash serializes the whole execution environment?
I guess dash serializes the whole execution environment?
Yes, we use multiprocess
to spawn the background process and that will try to transfer all the global variables.
Any updates @fxstempfelals as I encounter exactly your mentioned error, when applying a Background Callback ? I'm using py 3.11 and dash==2.17.0
@DJ2695 Nothing new on my side, I was thinking of reconsidering the whole design of my app but didn't have time yet to do so. But I'd be glad to know how you addressed this issue!
Describe your context
Windows 11, Python 3.9
Occurs with Edge & Chrome.
Describe the bug
I use a background callback in a multi-page app that throws an error when called:
TypeError: cannot pickle 'SSLContext' object
in the browser (without more details, see screenshot below)Here's how the callback is defined:
The callback involves an object that has a SQLAlchemy engine as an attribute. The connection is made through SSL, so I guess this is the object that fails to be pickled. However, I can serialize this object successfully with
dill.dumps
, so I'm not sure...Maybe related to https://github.com/uqfoundation/dill/issues/308, but until the issue is fixed, there might be a workaround?
Expected behavior
I expect the callback to run without error.
Screenshots