Expected Real-time plot, but hung..

Environment:

Python 3.6.5

Bokeh 0.12.16 (tornado 5.0.2)

Windows 10

Hi, I tried to make real-time dashboard to display the process
So I found the example in https://bokeh.pydata.org/en/latest/docs/user_guide/server.html#updating-from-unlocked-callbacks and try to find the anwser.

My code snippets is like this:

#-- coding:utf-8 --

from bokeh.plotting import figure, curdoc

from bokeh.models import ColumnDataSource

from bokeh.layouts import row

import time

from functools import partial

from tornado import gen

import numpy as np

class bspid_visual(object):

def __init__(self):

    self.index = 0

    self.data_list = dict()

    self.plot_list = dict()

    self.doc = curdoc()

def generate(self, key, color):

    self.data_list[key] = ColumnDataSource(data=dict(x=[0], y=[0], color=[color]))

    self.plot_list[key] = figure(plot_width=600, plot_height=600, title=key)

    self.plot_list[key].circle(x='x', y='y', source=self.data_list[key])

    print(key + " generated")

    # self.plot_list[key].circle(x='x', y='y', source=self.data_list[key], legend='circle')

@gen.coroutine

def report(self, key, data):

    self.data_list[key].data['x'].append(self.data_list[key].data['x'][-1] + 1)

    self.data_list[key].data['y'].append(data)

    self.data_list[key].data['color'].append(self.data_list[key].data['color'][-1])

    self.doc.add_next_tick_callback(partial(self.update, key))

def update(self, key):

    data_dict = dict(x=[self.data_list[key].data['x'][-1]], y=[self.data_list[key].data['y'][-1]], color=[self.data_list[key].data['color'][-1]])

    self.data_list[key].stream(data_dict)

    print("updated")

def register(self):

    self.doc.add_root(row(self.plot_list['Kp']))

def unit_test():

test = bspid_visual()

test.generate('Kp', 'red')

test.register()

for i in range(100):

    test.report('Kp', i)

    time.sleep(0.5)

unit_test()

``

So my intent is to display the plot this in every 0.5s. But when I run this, hung is occurred in somewhere.

Bokeh console log shows that:

2018-08-23 18:43:36,284 Starting Bokeh server version 0.12.16 (running on Tornado 5.0.2)

2018-08-23 18:43:36,297 Bokeh app running at: http://localhost:5006/bspid_visual_test

2018-08-23 18:43:36,297 Starting Bokeh server with process id: 6560

Kp generated

2018-08-23 18:44:27,116 200 GET /bspid_visual_test (::1) 50259.81ms

I`m just beginner of bokeh, so maybe there are some error is hiding. Could somebody help about this?

Thanks in advance.

Chanseok.

Hi,

The script you write to run on a Bokeh server is a bit more like a blueprint. The Bokeh server runs the code to create every new session, but more importantly, all the code is run *before* the session is displayed. If the script does any long or blocking execution (like yours does), then all that happens *before* anything can get displayed. If you want "periodic" things to happen, you can't do them directly in the script. You can either:

* define a callback, and set that up to run periodically with

     curdoc().add_periodic_callback(...)

* start a separate thread yourself that updates the Bokeh state by passing callbacks to

     curdoc().add_next_tick_callback(...)

Thanks

Bryan

···

On Aug 23, 2018, at 03:01, [email protected] wrote:

Environment:
Python 3.6.5
Bokeh 0.12.16 (tornado 5.0.2)
Windows 10

Hi, I tried to make real-time dashboard to display the process
So I found the example in https://bokeh.pydata.org/en/latest/docs/user_guide/server.html#updating-from-unlocked-callbacks and try to find the anwser.

My code snippets is like this:
#-*- coding:utf-8 -*-

from bokeh.plotting import figure, curdoc
from bokeh.models import ColumnDataSource
from bokeh.layouts import row

import time

from functools import partial

from tornado import gen

import numpy as np

class bspid_visual(object):
    def __init__(self):
        self.index = 0
        self.data_list = dict()
        self.plot_list = dict()
        self.doc = curdoc()

    def generate(self, key, color):
        self.data_list[key] = ColumnDataSource(data=dict(x=[0], y=[0], color=[color]))
        self.plot_list[key] = figure(plot_width=600, plot_height=600, title=key)
        self.plot_list[key].circle(x='x', y='y', source=self.data_list[key])
        print(key + " generated")
        # self.plot_list[key].circle(x='x', y='y', source=self.data_list[key], legend='circle')

    @gen.coroutine
    def report(self, key, data):
        self.data_list[key].data['x'].append(self.data_list[key].data['x'][-1] + 1)
        self.data_list[key].data['y'].append(data)
        self.data_list[key].data['color'].append(self.data_list[key].data['color'][-1])
        self.doc.add_next_tick_callback(partial(self.update, key))

    def update(self, key):
        data_dict = dict(x=[self.data_list[key].data['x'][-1]], y=[self.data_list[key].data['y'][-1]], color=[self.data_list[key].data['color'][-1]])
        self.data_list[key].stream(data_dict)
        print("updated")

    def register(self):
        self.doc.add_root(row(self.plot_list['Kp']))

def unit_test():
    test = bspid_visual()
    test.generate('Kp', 'red')
    test.register()

    for i in range(100):
        test.report('Kp', i)
        time.sleep(0.5)

unit_test()
    
So my intent is to display the plot this in every 0.5s. But when I run this, hung is occurred in somewhere.
Bokeh console log shows that:

2018-08-23 18:43:36,284 Starting Bokeh server version 0.12.16 (running on Tornado 5.0.2)
2018-08-23 18:43:36,297 Bokeh app running at: http://localhost:5006/bspid_visual_test
2018-08-23 18:43:36,297 Starting Bokeh server with process id: 6560
Kp generated
2018-08-23 18:44:27,116 200 GET /bspid_visual_test (::1) 50259.81ms

I`m just beginner of bokeh, so maybe there are some error is hiding. Could somebody help about this?

Thanks in advance.
Chanseok.

--
You received this message because you are subscribed to the Google Groups "Bokeh Discussion - Public" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/bokeh/8bf9920d-22c6-4c79-a5d4-72008347b715%40continuum.io.
For more options, visit https://groups.google.com/a/continuum.io/d/optout.

Thanks for comment.

I`ve already concerned about two case you mentioned:

  • first, before adding my plot in root layout, I register my plotting method(self.update) periodically,and update the dataset in separate thread

#-- coding:utf-8 --

from bokeh.plotting import figure, curdoc

from bokeh.models import ColumnDataSource

from bokeh.layouts import row

import time

from functools import partial

from tornado import gen

import numpy as np

from threading import Thread

class bspid_visual(object):

def __init__(self):

    self.index = 0

    self.data_list = dict()

    self.plot_list = dict()

    self.doc = curdoc()

def generate(self, key, color):

    self.data_list[key] = ColumnDataSource(data=dict(x=[0], y=[0], color=[color]))

    self.plot_list[key] = figure(plot_width=600, plot_height=600, title=key)

    self.plot_list[key].circle(x='x', y='y', source=self.data_list[key])

    print(key + " generated")

    # self.plot_list[key].circle(x='x', y='y', source=self.data_list[key], legend='circle')

def report(self, key, data):

    self.data_list[key].data['x'].append(self.data_list[key].data['x'][-1] + 1)

    self.data_list[key].data['y'].append(data)

    self.data_list[key].data['color'].append(self.data_list[key].data['color'][-1])

    #self.doc.add_next_tick_callback(partial(self.update, key))

    print("report")

def update(self, key):

    data_dict = dict(x=[self.data_list[key].data['x'][-1]], y=[self.data_list[key].data['y'][-1]], color=[self.data_list[key].data['color'][-1]])

    self.data_list[key].stream(data_dict)

    print("updated")

def register(self):

    **self.doc.add_periodic_callback(partial(self.update, 'Kp'), 1000)**

    self.doc.add_root(row(self.plot_list['Kp']))

def unit_test(test):

test.generate('Kp', 'red')

test.register()

def thread_test(test):

for i in range(100):

    test.report('Kp', i)

    time.sleep(0.5)

test = bspid_visual()

unit_test(test)

thread = Thread(target=thread_test, args=(test,))

thread.start()

thread.join()

``

-> in this case, plotting method is blocking in somewhere and plot it suddenly(almost 50s after execution)

  • and I also tried to add plotting method callback right after updating data (using curdoc().add_next_tick_callback())

#-- coding:utf-8 --

from bokeh.plotting import figure, curdoc

from bokeh.models import ColumnDataSource

from bokeh.layouts import row

import time

from functools import partial

from tornado import gen

import numpy as np

from threading import Thread

class bspid_visual(object):

def __init__(self):

    self.index = 0

    self.data_list = dict()

    self.plot_list = dict()

    self.doc = curdoc()

def generate(self, key, color):

    self.data_list[key] = ColumnDataSource(data=dict(x=[0], y=[0], color=[color]))

    self.plot_list[key] = figure(plot_width=600, plot_height=600, title=key)

    self.plot_list[key].circle(x='x', y='y', source=self.data_list[key])

    print(key + " generated")

    # self.plot_list[key].circle(x='x', y='y', source=self.data_list[key], legend='circle')

def report(self, key, data):

    self.data_list[key].data['x'].append(self.data_list[key].data['x'][-1] + 1)

    self.data_list[key].data['y'].append(data)

    self.data_list[key].data['color'].append(self.data_list[key].data['color'][-1])

    print("report")

def update(self, key):

    data_dict = dict(x=[self.data_list[key].data['x'][-1]], y=[self.data_list[key].data['y'][-1]], color=[self.data_list[key].data['color'][-1]])

    self.data_list[key].stream(data_dict)

    print("updated")

def register(self):

    # self.doc.add_periodic_callback(partial(self.update, 'Kp'), 1000)

    self.doc.add_root(row(self.plot_list['Kp']))

def unit_test(test):

test.generate('Kp', 'red')

test.register()

for i in range(100):

    test.report('Kp', i)

    time.sleep(0.5)

def thread_test(test):

for i in range(100):

    test.doc.add_next_tick_callback(partial(test.update, 'Kp'))

    time.sleep(0.5)

test = bspid_visual()

thread = Thread(target=thread_test, args=(test,))

thread1 = Thread(target=unit_test, args=(test, ))

thread1.start()

thread.start()

thread1.join()

thread.join()

``

-> same thing happens. (plotting method is blocking and plot it suddenly)

I`ll try it in another ways in terms of thread.

Thanks.

Chanseok.

2018년 8월 24일 금요일 오전 1시 19분 39초 UTC+9, Bryan Van de ven 님의 말:

···

Hi,

The script you write to run on a Bokeh server is a bit more like a blueprint. The Bokeh server runs the code to create every new session, but more importantly, all the code is run before the session is displayed. If the script does any long or blocking execution (like yours does), then all that happens before anything can get displayed. If you want “periodic” things to happen, you can’t do them directly in the script. You can either:

  • define a callback, and set that up to run periodically with

    curdoc().add_periodic_callback(…)

  • start a separate thread yourself that updates the Bokeh state by passing callbacks to

    curdoc().add_next_tick_callback(…)

Thanks

Bryan

On Aug 23, 2018, at 03:01, [email protected] wrote:

Environment:

Python 3.6.5

Bokeh 0.12.16 (tornado 5.0.2)

Windows 10

Hi, I tried to make real-time dashboard to display the process

So I found the example in https://bokeh.pydata.org/en/latest/docs/user_guide/server.html#updating-from-unlocked-callbacks and try to find the anwser.

My code snippets is like this:

#-- coding:utf-8 --

from bokeh.plotting import figure, curdoc

from bokeh.models import ColumnDataSource

from bokeh.layouts import row

import time

from functools import partial

from tornado import gen

import numpy as np

class bspid_visual(object):

def __init__(self):
    self.index = 0
    self.data_list = dict()
    self.plot_list = dict()
    self.doc = curdoc()
def generate(self, key, color):
    self.data_list[key] = ColumnDataSource(data=dict(x=[0], y=[0], color=[color]))
    self.plot_list[key] = figure(plot_width=600, plot_height=600, title=key)
    self.plot_list[key].circle(x='x', y='y', source=self.data_list[key])
    print(key + " generated")
    # self.plot_list[key].circle(x='x', y='y', source=self.data_list[key], legend='circle')
@gen.coroutine
def report(self, key, data):
    self.data_list[key].data['x'].append(self.data_list[key].data['x'][-1] + 1)
    self.data_list[key].data['y'].append(data)
    self.data_list[key].data['color'].append(self.data_list[key].data['color'][-1])
    self.doc.add_next_tick_callback(partial(self.update, key))
def update(self, key):
    data_dict = dict(x=[self.data_list[key].data['x'][-1]], y=[self.data_list[key].data['y'][-1]], color=[self.data_list[key].data['color'][-1]])
    self.data_list[key].stream(data_dict)
    print("updated")
def register(self):
    self.doc.add_root(row(self.plot_list['Kp']))

def unit_test():

test = bspid_visual()
test.generate('Kp', 'red')
test.register()
for i in range(100):
    test.report('Kp', i)
    time.sleep(0.5)

unit_test()

So my intent is to display the plot this in every 0.5s. But when I run this, hung is occurred in somewhere.

Bokeh console log shows that:

2018-08-23 18:43:36,284 Starting Bokeh server version 0.12.16 (running on Tornado 5.0.2)

2018-08-23 18:43:36,297 Bokeh app running at: http://localhost:5006/bspid_visual_test

2018-08-23 18:43:36,297 Starting Bokeh server with process id: 6560

Kp generated

2018-08-23 18:44:27,116 200 GET /bspid_visual_test (::1) 50259.81ms

I`m just beginner of bokeh, so maybe there are some error is hiding. Could somebody help about this?

Thanks in advance.

Chanseok.


You received this message because you are subscribed to the Google Groups “Bokeh Discussion - Public” group.

To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].

To post to this group, send email to [email protected].

To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/bokeh/8bf9920d-22c6-4c79-a5d4-72008347b715%40continuum.io.

For more options, visit https://groups.google.com/a/continuum.io/d/optout.