Avoiding pending writes and single document error when using CDS

Is there any way to outsource CDS, functions etc. into external files/modules in mentioned style above without getting pending writes and/or single document errors?

You would have to put the creation inside a function, and then have the app code call that function, e.g.:

# ext_cds.py

def get_cds():
    return ColumnDataSource(...)

The purpose of Bokeh application code (whether its a script, or a notebook, or passed to a function handler) is to create a new, unique set of Bokeh models for every session. To this end, he app code is executed every time a session starts. But Python’s module caching means that module-scope (“global”) Bokeh objects in external modules cannot work, because those objects would be cached and re-used between sessions (which does not work/is not allowed). We can’t change Python’s module caching behavior, so this situation is not going to change .

If you want to share large non-Bokeh data structures in modules between sessions, e.g. large arrays or DataFrames, that can be fine (but I recommend doing so only with read-only data). The spectrogram example demonstrates this. A CDS is a very thin wrapper around such things. You just have to make a new CDS so that each user has their own unique Bokeh objects for their own session. Bokeh objects cannot be shared between sessions. [1]

Can one destroy all (bokeh-)objects immediately after reloading/closing a page?

Bokeh objects are referred to from sessions, which are periodically reaped. They will be collected by normal Python GC after the session is expired. You can change the session expiration options but of course checking for unused sessions more frequently can have its own trade-offs, depending on the number of sessions you typically have, and how long they last, etc.

  • Why is this intended behavior and what’s the probem about destroying all objects of a single session/port when reloading/closing it, since you need all new objects anyway? Will this be fixed in a future release?

I don’t really understand what you are asking. Your issues seems all to be with not creating new objects for every session, and not anything to do with when session objects are released.


  1. To be crystal clear about why Bokeh models cannot be shared between sessions: Imagine another user pans a plot and your view changes as a result, because a range is shared. Or worse, another user has access to sensitive data in your session because a CDS is shared. Sharing objects between sessions is inherently unsafe and surprising and cannot be permitted. ↩︎