3
我想了解如何使用散景服務器來顯示使用套接字的實時情節。到目前爲止,我創建了兩個文件運行到兩個單獨的窗口中,以模擬要發送的數據流。實時散景插座蟒
我有發送包含3個元素的陣列的client.py:
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