我正在开发一个模拟示波器,服务器 PC 收集数据并最终在线发布流图。下面是一个可以完成这项工作的工作脚本。但是,当我打开多个浏览器时,流图会显示不同的数据。 (尽管他们使用相同的数据源)。示例“ohlc”似乎也有同样的问题。那么,这样做的正确方法是什么?我正在考虑将数据写入文件,但这会带来一些问题,例如文件 I/O 延迟和磁盘存储限制等。谢谢您的帮助。
from bokeh.server.server import Server
from bokeh.models import ColumnDataSource, Label
from bokeh.plotting import figure
from bokeh.layouts import column
import numpy as np
import datetime as dt
from functools import partial
import time
# this will be replaced with the real data collector in the end
def f_emitter(p=0.1):
v = np.random.rand()
return (dt.datetime.now(), 0. if v>p else v)
def make_document(doc, functions, labels):
def update():
for index, func in enumerate(functions):
data = func()
sources[index].stream(new_data=dict(time=[data[0]], data=[data[1]]), rollover=1000)
annotations[index].text = f'{data[1]: .3f}'
sources = [ColumnDataSource(dict(time=[], data=[])) for _ in range(len(functions))]
figs = []
annotations = []
for i in range(len(functions)):
figs.append(figure(x_axis_type='datetime',
y_axis_label=labels[i], toolbar_location=None,
active_drag=None, active_scroll=None))
figs[i].line(x='time', y='data', source=sources[i])
annotations.append(Label(x=10, y=10, text='', text_font_size='40px', text_color='black',
x_units='screen', y_units='screen', background_fill_color='white'))
figs[i].add_layout(annotations[i])
# print(figs[i].plot_height)
doc.add_root(column([fig for fig in figs], sizing_mode='stretch_both'))
doc.add_periodic_callback(callback=update, period_milliseconds=100)
if __name__ == '__main__':
# list of functions and labels to feed into the scope
functions = [f_emitter]
labels = ['emitter']
server = Server({'/': partial(make_document, functions=functions, labels=labels)})
server.start()
server.io_loop.add_callback(server.show, "/")
try:
server.io_loop.start()
except KeyboardInterrupt:
print('keyboard interruption')
最佳答案
当您连接新客户端时,Bokeh 默认情况下会创建一个新 session 。每个 session 都有自己的文档,因此数据源最终不会相同。
关于windows-10 - 单个 Bokeh 服务器不同的 session 显示不同的流数据,如何解决?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61621111/