Hi Bokeh Community.
I recently posted this on the github issue tracker (#4071) but I fear my concern of a potential issue might be more of my coding error. Here it goes…
When running a the following command “bokeh serve --show server1.py” my add_root() method seems to hang in between client-server communication. I’ve attempted not using the “bokeh serve” command and instead incorporating "session = push_session(curdoc()) … session.loop_until_closed() " but that hasn’t produced a solution. When running with “bokeh serve…” on multiple attempts, the client socket connection does not close and the server seems to hang.
I’d like to be able to have the bokeh server running and constantly be creating client socket connections that send, update/add to the existing data, and close.
@birdsarah had a great response to @dwf in github issue #3087 and that has helped me out so #far…
Here’s the bokeh serve command that I execute first "bokeh serve --show server1.py
from socket import *
from time import ctime
HOST = 'localhost'
PORT = 5006
BUFSIZ = 2048
ADDR = (HOST, PORT)
tcpSerSock = socket(AF_INET, SOCK_STREAM)
tcpSerSock.bind(ADDR)
tcpSerSock.listen(5)
import numpy as np
from bokeh.palettes import RdYlBu3
from bokeh.plotting import figure, curdoc, vplot
p = figure(x_range=(0, 100), y_range=(0, 100), toolbar_location=None)
p.border_fill_color = 'black'
p.background_fill_color = 'black'
p.outline_line_color = None
p.grid.grid_line_color = None
r = p.text(x=[], y=[], text=[], text_color=[], text_font_size="20pt",
text_baseline="middle", text_align="center")
ds = r.data_source
def update(i):
i = int(i)
ds.data['x'].append(np.random.random()*70 + 15)
ds.data['y'].append(np.random.random()*70 + 15)
ds.data['text_color'].append(RdYlBu3[i%3])
ds.data['text'].append(str(i))
ds.trigger('data', ds.data, ds.data)
while True:
print('waiting for connection...')
tcpCliSock, addr = tcpSerSock.accept()
print('connected from: ', addr)
while True:
data = tcpCliSock.recv(BUFSIZ)
if not data:
break
rdata = data.decode('utf-8')
response = '[%s] %s' % (ctime(), rdata)
curdoc().add_root(update(rdata), p)
print("rdata:", rdata)
print("ds:", ds.data['x'], ds.data['y'])
print("p:", p)
tcpCliSock.send(response.encode('utf-8'))
print(response)
tcpCliSock.close()
tcpSerSock.close()
``
``
Here’s the client command that I run second to send data “python client1.py” :
from socket import *
HOST = 'localhost'
PORT = 5006
BUFSIZ = 2048
ADDR = (HOST, PORT)
tcpCliSock = socket(AF_INET, SOCK_STREAM)
tcpCliSock.connect(ADDR)
data = '5'
tcpCliSock.send(data.encode('utf-8'))
data = tcpCliSock.recv(BUFSIZ)
print(data.decode('utf-8'))
tcpCliSock.close()
``
When at the “localhost…” url in my web browser, the figure loads and I see what I want. However, the connection hangs and won’t close when attempting to send data for a second time. Again, I’d like to be able to have the bokeh server running and constantly be creating client socket connections that send, update/add to the existing data, and close. Repeat. Is this an issue or my coding error? This is may not be an issue and just a misunderstanding of my introductory knowledge of bokeh serve.
Thanks for your time and consideration in help!