Различные сеансы показывают разные потоковые данные с одним сервером боке, как решить эту проблему? - PullRequest
0 голосов
/ 05 мая 2020

Я работаю над смоделированным осиллографом, где сервер P C собирает данные и в конечном итоге публикует sh график потоковой передачи в Интернете. Ниже приведен рабочий сценарий, который может выполнить эту работу. Однако, когда я открываю несколько браузеров, графики потоковой передачи показывают разные данные. (Хотя они используют один и тот же источник данных). Пример "ohl c", похоже, имеет ту же проблему. Итак, как правильно это сделать? Я подумываю записать данные в файл, но это вызовет некоторые проблемы, такие как задержка ввода-вывода файла, ограничение дискового пространства и т. Д. c. Спасибо за любую помощь.

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')

1 Ответ

1 голос
/ 05 мая 2020

Когда вы подключаетесь к новому клиенту, по умолчанию Bokeh создает новый сеанс. У каждого сеанса есть свой собственный документ, поэтому источники данных в конечном итоге не совпадают.

...