我使用的过程通信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,因此无法弄清楚这一点,有人可以向我解释吗?

提前致谢
s

最佳答案

在将套接字转换为类似文件的对象(connFile = conn.makefile())时,套接字中的所有内容在文件中都可用。当您第一次执行cPickle.load(connFile)时,套接字/文件中的所有内容都由pickle加载,因此在第二次调用中,没有任何东西需要释放,这就是pickle在引发异常时所提示的。

您的所有数据都不会在第一个cPickle.load(connFile)上被剔除,如果您要提取函数名,args和kwargs,请尝试发送包含这些数据的元组:

客户端 :

cPickle.dump((function_name, args, kwargs), client_socket_as_file)

服务器端
name, args, kwargs = cPickle.load(connFile)

09-04 07:01