windows-10 - 单个 Bokeh 服务器不同的 session 显示不同的流数据,如何解决?

标签 windows-10 bokeh python-3.7

我正在开发一个模拟示波器,服务器 PC 收集数据并最终在线发布流图。下面是一个可以完成这项工作的工作脚本。但是,当我打开多个浏览器时,流图会显示不同的数据。 (尽管他们使用相同的数据源)。示例“ohlc”似乎也有同样的问题。那么,这样做的正确方法是什么?我正在考虑将数据写入文件,但这会带来一些问题,例如文件 I/O 延迟和磁盘存储限制等。谢谢您的帮助。

enter image description here enter image description here

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/

相关文章:

css - 删除 Microsoft Edge(以前称为 Project Spartan)中的 'x' 输入装饰

python - 使用 Tornado 显示 Bokeh 生成的文件

python - 编程式 Bokeh 服务器

regex - 从文件中获取列名称列表的精确匹配

c# - 枚举值的 GetCustomAttributes 返回一个空数组

java - 启动错误: could not create the Java virtual machine.发生致命异常

winapi - 有没有办法从 Win32 应用程序检测 Windows 10 中 Focus Assist(以前称为 Quiet Hours)的变化

python - 将 Band 对象添加到 Bokeh 中的图例?

python - SSLError ("Can' t 连接到 HTTPS URL,因为 SSL 模块不可用。”)在 pip 命令中

python - 如何在函数内部打印 __init__() ?