Live data reading and processing

I have been trying to implement a live data viewer (spectrometer) in a bokeh server. I have been somewhat successful, but now I’m running into problems, to the point I am not sure if bokeh is even the right path.

What has worked so far: I have a server with a few plots, and a periodic callback. I estimate the sensor readout time (lets say 50 ms) and make the loop time for the periodic callback slightly higher to account for some data processing (lets say 60 ms). This works nicely and I get a live view of my spectrum.

However: The problems come, when I try to implement callbacks for buttons etc. If I press a button, the callback only gets executed a couple seconds later. What I want is an immediate reaction (to freeze the current spectrum for example).

What I’ve tried: Increasing the loop time only had a minor effect. I tried threading similar to this post. I made a seperate thread to readout the data + save in a data structure. This worked for very slow readout times (eg. 1 s), but at fast readouts (50 ms) everything was way worse.

At this point I am not sure how to continue. I looked into asyncio. Maybe multiprocessing would be an option too. Maybe there is an even easier way. I’ve also looked into add_next_tick_callback, but I’m not sure if this ist the right path. Any advice would be appreciated.

Hi @jonathansailing it’s not really possible to speculate, especially on something like this, without actual code (i.e. a complete Minimal Reproducible Example).

Otherwise, all I can really do is point at the existing spectrogram example in the examples repo, which has sliders that (at least IMO) are responsive. Perhaps its structure could be informative.

https://github.com/bokeh/bokeh/tree/branch-3.4/examples/server/app/spectrogram

That example has the tight loop simply refer to the widget value, rather than try to interpose a callback execution. Possibly a similar approach could be made with a toggle state rather than a button event.

Did you ever find a solution to this? Was okayish in 3.3.0 but now that I’ve upgraded to 3.3.4 the buttons seem to be unusable after having the periodic callback run for a certain amount of time.