实时散景套接字python

时间:2015-10-20 15:52:47

标签: python sockets real-time 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,它接收数据并更新散景服务器中的图。

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

我确认我已经运行了散景服务器。我收到以下错误。通过故障排除我相信这个错误以某种方式发生,因为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

1 个答案:

答案 0 :(得分:0)

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