简体   繁体   English

实时散景插座python

[英]Real-time bokeh socket python

I'm trying to understand how to work with bokeh-server to display a real-time plot using sockets. 我试图了解如何使用散景服务器来显示使用套接字的实时绘图。 So far I created two files to run into two separate windows in order to simulate the stream of data to be sent. 到目前为止,我创建了两个文件来运行到两个单独的窗口,以模拟要发送的数据流。

I have a client.py that sends an array containing 3 elements: 我有一个client.py发送一个包含3个元素的数组:

import sys
import time
import socket
from pylab import randn


# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Connect the socket to the port where the server is listening
server_address = ('localhost', 2000)
sock.connect(server_address)

try:
    for i in xrange(100):
        m = randn(3)
        message = m.tostring()
        print >>sys.stderr, 'sending "%s"' % m
        sock.sendall(message)
        time.sleep(0.01)

finally:
    print >>sys.stderr, 'closing socket'
    sock.close()

I have also a server.py which receives the data and updates the plot in the bokeh-server. 我还有一个server.py接收数据并更新散景服务器中的绘图。

import sys
import time
import socket
from pylab import *
from bokeh.plotting import cursession, figure, show, output_server


output_server("raw_values")

# Visualization workflow
counter = 0
rx = [0]
ry = [0]
rz = [0]
p = figure()
x = [0]
p.line(x, rx, name='raw_mx')

# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Bind the socket to the port
server_address = ('localhost', 2000)
print >>sys.stderr, 'starting up on %s port %s' % server_address
sock.bind(server_address)

# Listen for incoming connections
sock.listen(1)

renderer = p.select(dict(name="raw_mx"))
ds = renderer[0].data_source

show(p)

while True:
    # Wait for a connection
    print >>sys.stderr, 'waiting for a connection'
    connection, client_address = sock.accept()

    try:
        print >>sys.stderr, 'connection from', client_address

        while True:
            data = fromstring(connection.recv(24))
            print data

            if len(data) == 0:
                break

            rx += [data[0]]
            ry += [data[1]]
            rz += [data[2]]
            x += [counter]
            counter += 1

            ds.data["x"] = x
            ds.data["rmag_x"] = rx
            cursession().store_objects(ds)
            time.sleep(0.01)


    finally:
        # Clean up the connection
        connection.close()

I do confirm I have the bokeh-server running. 我确认我已经运行了散景服务器。 I'm receiving the following error. 我收到以下错误。 By troubleshooting I believe this errors occurs somehow due to the show(p) as if I comment that line I have no error, the data is sent and received. 通过故障排除我相信这个错误以某种方式发生由于show(p) ,好像我评论该行我没有错误,数据被发送和接收。 However, the plot is not updated since the show(p) is not present. 但是,由于show(p)不存在,因此不会更新绘图。

--------------------------------------------------------------------------
error                                     Traceback (most recent call last)
/home/user/Documents/server.py in <module>()
     36     # Wait for a connection
     37     print >>sys.stderr, 'waiting for a connection'
---> 38     connection, client_address = sock.accept()
     39 
     40     try:

/home/user/anaconda/lib/python2.7/socket.pyc in accept(self)
    204 
    205     def accept(self):
--> 206         sock, addr = self._sock.accept()
    207         return _socketobject(_sock=sock), addr
    208     accept.__doc__ = _realsocket.accept.__doc__

error: [Errno 4] Interrupted system call

In [38]: 
(process:13045): GLib-CRITICAL **: g_slice_set_config: assertion 'sys_page_size == 0' failed

The error was due to some network settings misconfiguration that were preventing the server to work properly. 该错误是由于某些网络设置配置错误导致服务器无法正常工作。 The code is working nice right now. 代码现在很好用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM