我使用过程通信RMC,其代码可以在下面找到:
HOST = '' # local host
PORT = 50000
SERVER_ADDRESS = HOST, PORT
# set up server socket
s = socket.socket()
s.bind(SERVER_ADDRESS)
s.listen(1)
while True:
conn, addr = s.accept()
connFile = conn.makefile()
name, args, kwargs = cPickle.load(connFile)
'''name = cPickle.load(connFile)
args = cPickle.load(connFile)
kwargs = cPickle.load(connFile)'''
res = _exportedMethods[name](*args,**kwargs)
cPickle.dump(res,connFile) ; connFile.flush()
conn.close()
这是客户端网站:
class RemoteFunction(object):
def __init__(self,serverAddress,name):
self.serverAddress = serverAddress
self.name = name
def __call__(self,*args,**kwargs):
s = socket.socket()
s.connect(self.serverAddress)
f = s.makefile()
cPickle.dump((self.name, args, kwargs), f)
'''cPickle.dump(self.name,f)
cPickle.dump(args,f)
cPickle.dump(kwargs,f)'''
f.flush()
res = cPickle.load(f)
s.close()
return res
def machine_changed_signal(machine):
HOST = ''
PORT = 50000
SERVER_ADDRESS = HOST, PORT
advise = RemoteFunction(SERVER_ADDRESS,'changes')
advise(machine)
执行时,我收到以下错误消息:
Traceback (most recent call last):
File "/home/manch011/disserver/src/disserver/gui/backends/receiver.py", line 71, in run
args = cPickle.load(connFile)
cPickle.UnpicklingError: pickle data was truncated
我的更改后,新的我收到以下错误消息:
Traceback (most recent call last):
File "/home/manch011/disserver/src/disserver/gui/backends/receiver.py", line 69, in run
name, args, kwargs = cPickle.load(connFile)
EOFError
我对cPickle并不熟悉,因此无法解决这个问题,有人可以向我解释一下吗?
提前致谢 CHIS
答案 0 :(得分:3)
在类似文件的对象(connFile = conn.makefile()
)中转换套接字时,套接字中的所有内容都可以在文件中找到。当你第一次cPickle.load(connFile)
时,你的套接字/文件中的所有内容都被pickle加载,所以在第二次调用时,没有什么可以解开,这就是pickle在提出异常时所抱怨的。 / p>
如果你想提取函数名,args和kwargs,尝试发送包含这些数据的元组,那么你的所有数据都会在第一个cPickle.load(connFile)
上被取消。
客户端:
cPickle.dump((function_name, args, kwargs), client_socket_as_file)
服务器侧
name, args, kwargs = cPickle.load(connFile)