Thread (non) safety

I am trying to run a complex bokeh app, with many people collecting to the same one. What I see happening is spill over between the different sessions. I have since retrenched all bokeh calls as far as possible to a single file, which means some of my nifty classes have now disappeared.
However, that I still have issues it shows I don’t understand where threads appear in the bokeh server, and how information is shared between such threads (probably a misunderstanding on my side of where the state of modules and classes is held in python). Can anyone point me at a place I can read up on this?
Niels

The quick answer to that is: nowhere. Bokeh server does not have any threading of its own. [1]

The short version of the requirement is: Bokeh objects should not be shared between sessions, full stop. The quickest way to run afoul of that is to have Bokeh objects that are (or contained in) module-globals somewhere. This results in sharing, because of the way Python caches modules. It’s fine to modularize your app or have classes, but you need to make sure that any Bokeh models that you add to curdoc are newly created objects, every time.

it’s not really possible to speculate in any detail about your specific situation without a complete Minimal Reproducible Example that could actually be run to investigate.


  1. There’s been ideas about moving session creation to threads or adding a convenience for a threaded callback executor, but no work has been done to date. ↩︎

Thanks Brian,
Will have another trawl for module globals–I seem to see issues left right and center (mainly linked to externally defined modules). It may be that more of the information that each model depends on needs to localised as well.
Thanks again,
Niels

"having issues’ is pretty vague and unspecific, unfortunately. If you are trying to update things from your own threads (unclear) then you should certainly read

https://docs.bokeh.org/en/latest/docs/user_guide/server.html#updating-from-threads

No I was not doing my own threads: I just had way too many module globals, etc.–and sorry I was vague!
In the end it was localising the data to the “main” application that solved this. I think I have one minor problem left, which may point to a module generating a small memory leak as well, but I have made real progress.
It was interesting to see how quickly I could transfer from a static python pdf-based data dashboard to an interactive web-based dashboard, which helps our process of moderating many 100s of university modules in a very short time enormously!

2 Likes