Caching of Bokeh Plots : Models must be owned by only a single document, Line(id='2385', ...) is already in a doc

I’ve a dashboard and i generate the plots when user access the dashboard for the first time and cache the object . Now next when when user comes back with same url i want to return the cached object but getting following error :

Returning from cache defaultdict(<class 'dict'>, {'666b5bf7-8f1a-46d4-96d5-3e510108fd6c_Diag_aligned_data_json_Temp': {'BLER': Panel(id='2573', ...)}})
Done Processing ...
bokeh.application.application - ERROR - Error running application handler <bokeh.application.handlers.directory.DirectoryHandler object at 0x7f85ae18a990>: Models must be owned by only a single document, Line(id='2385', ...) is already in a doc
File "model.py", line 671, in _attach_document:
raise RuntimeError("Models must be owned by only a single document, %r is already in a doc" % (self)) Traceback (most recent call last):
  File "/home/hadoop/miniconda/lib/python3.7/site-packages/bokeh/application/handlers/code_runner.py", line 197, in run
    exec(self._code, module.__dict__)
  File "/home/hadoop/bokeh_app/main.py", line 15, in <module>
    dashboard(doc, getClient(), job_id=job_id)
  File "/home/hadoop/bokeh_app/scripts/dashboard.py", line 286, in dashboard
    doc.add_root(column(theme_select, adv_tabs))
  File "/home/hadoop/miniconda/lib/python3.7/site-packages/bokeh/document/document.py", line 319, in add_root
    self._pop_all_models_freeze()
  File "/home/hadoop/miniconda/lib/python3.7/site-packages/bokeh/document/document.py", line 1056, in _pop_all_models_freeze
    self._recompute_all_models()
  File "/home/hadoop/miniconda/lib/python3.7/site-packages/bokeh/document/document.py", line 1079, in _recompute_all_models
    a._attach_document(self)
  File "/home/hadoop/miniconda/lib/python3.7/site-packages/bokeh/model.py", line 671, in _attach_document
    raise RuntimeError("Models must be owned by only a single document, %r is already in a doc" % (self))
RuntimeError: Models must be owned by only a single document, Line(id='2385', ...) is already in a doc

any suggestions ?

Don’t cache the models. Cache the data and recreate the models.
Or, if you want for the user to be able to continue where they left off, you should be able to preserve session ID and make sure that Bokeh waits for quite some time before cleaning up unused sessions.

Any example please ? I’ve been looking for something like that

Sorry, I don’t have anything at hand since I’ve never done it myself.

Any suggestions or documentation ? I see Bokeh is very poor when it comes to backend caching and performance wise when i use datashade with more than 1m data points

Bokeh is very poor when it comes to backend caching

I’d say that’s because it’s completely out of scope of Bokeh. Caching is an immensely complex topic - no matter what strategy ends up being implemented, there will be people unhappy with the decision. It’s best to just not implement it at all and make Bokeh open enough to let users do any caching they want themselves. Except for instances of Bokeh models, as you have found - they’re not shareable for very good reasons, and they’re cheap to create so it’s not a big deal.

performance wise when i use datashade with more than 1m data points

I assume you mean datashader. Well, it’s a separate project - nothing Bokeh can do about it.

Any suggestions or documentation ?

I might be wrong but I don’t think it’s documented - maybe because it has never been an intended usage. You can find some mentions of session IDs on https://docs.bokeh.org/en/latest/docs/user_guide/server.html and for the details I personally would go straight to the source code.