我使用远程过程调用在两个进程之间进行通信。我将手边的对象发送到另一个对象。该对象是Django模型的对象。该对象具有不同的变量,整数和字符串。

如果我只更改整数变量,则一切正常。如果我第一次更改字符串变量也可以,但是如果我第二次更改字符串,我的代码就会崩溃,并且出现以下错误消息

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)
cPickle.UnpicklingError: pickle data was truncated

这是我的代码,
在服务器端:
_exportedMethods = {
    'changes': signal_when_changes,
}

class ServerThread(QtCore.QThread):

    def __init__(self):
        super(ServerThread,self).__init__()
        st = self
    #threading.Thread.__init__(self)
    def run(self):
        HOST = ''     # local host
        PORT = 50000
        SERVER_ADDRESS = HOST, PORT

        # set up server socket
        s = socket.socket()
        s.bind(SERVER_ADDRESS)
        s.listen(5)

        while True:
            conn, addr = s.accept()
            connFile = conn.makefile()
            name, args, 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)
    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)

我不熟悉cPickle,因此无法弄清楚这一点,有人可以向我解释吗?

在此先感谢Chis

最佳答案

我解决了自己的问题。但是首先,我在问题中描述的错误消息没有意义。

我是新解决的问题,已经使用了Pyro4框架。所以我收到了一条新错误消息,该消息与旧错误消息相当,但很明显。不能 pickle 类的对象。
因为我只需要属性值,所以我将其传递到一个简单的字典中。

首先下载Pyro4并安装
一个简单的示例,类似于Pyro homepage上的示例:

# saved as helloworld.py
import Pyro4
import threading
import os
class HelloWorld(object):
    def get_hello_world(self, name):
        return "HelloWorld,{0}.".format(name)

#The NameServer had to run in a own thread because he has his own eventloop
class NameServer(threading.Thread):
    def __init__(self):
    threading.Thread.__init__(self)
    def run(self):
    os.system("python -m Pyro4.naming")
ns = NameServer()
ns.start()
hello_world=HelloWorld()
daemon=Pyro4.Daemon()                 # make a Pyro daemon
ns=Pyro4.locateNS()                   # find the name server
uri=daemon.register(hello_world)   # register the greeting object as a Pyro object
ns.register("example.helloworld", uri)  # register the object with a name in the name server
print "Ready."
daemon.requestLoop()                  # start the event loop of the server to wait for calls

运行该程序,然后执行下一个
# saved as client.py
import Pyro4
name=raw_input("What is your name? ").strip()
helloworld=Pyro4.Proxy("PYRONAME:example.helloworld")    # use name server object lookup uri shortcut
print helloworld.get_hello_world(name)

重要事项您不能传输类实例。因此,“名称”不能是类实例。

10-05 20:17