cPickle.UnpicklingError:pickle数据被截断

时间:2011-11-21 09:56:18

标签: python sockets django-models rpc pickle

我使用远程过程调用来在两个prozes之间进行通信。我将物体从现场发送到另一个。该对象是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

1 个答案:

答案 0 :(得分:1)

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

我解决了新问题并使用了Pyro4 Framework。所以我收到了一条新的错误信息,相当于旧的但很清楚。你不能腌制类对象。 因为在我的情况下我只需要在简单字典中传递的属性值。

首先下载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)

重要的是,您无法转移类实例。所以“name”不能是类实例。