我试图了解如何与bokeh服务器一起使用套接字显示实时绘图。到目前为止,我创建了两个文件以分别运行到两个单独的窗口中,以模拟要发送的数据流。

我有一个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()

我还有一个server.py,它接收数据并更新bokeh服务器中的图。
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()

我确认我正在运行bokeh服务器。我收到以下错误。通过故障排除,我相信由于show(p)会以某种方式发生此错误,就像我注释该行没有错误一样,数据已发送和接收。但是,由于不存在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

最佳答案

该错误是由于某些网络设置配置错误导致服务器无法正常工作。该代码现在运行良好。

关于python - 实时bokeh套接字python,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33241229/

10-13 03:30