- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我使用远程过程调用在两个过程之间进行通信。我将对象从手上发送到另一个。该对象是 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)
重要你不能传输类实例。所以“名称”不能是类实例。
关于python - cPickle.UnpicklingError : pickle data was truncated,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8209956/
假设我有一个 A 类和一个派生自 A 的 B 类。我想 pickle/unpickle B 类的一个实例。A 和 B 都定义了 __getstate__/__setstate__ 方法(假设 A
似乎有两种方法可以将指标推向 Graphite /碳, 线路接收器 pickle 接收器 根据文档 http://graphite.readthedocs.org/en/1.0/feeding-car
Perforce命令行有一个特殊的开关-G,它使用python的“pickle”序列化格式可以使输出成为机器可读的。一般来说,实际上是这样吗? 例如,考虑p4 -G diff -duw3 的输出。
如何从 BytesIO 对象写入和读回 pickled 数据? 我尝试过: import io import cPickle as pickle s1 = "foo" bytes_io = io.By
我有两个文件: x.py class BF(object) def __init__(): . . def add(self,z): . . y.py from y
在 post昨天发帖,无意中发现改了__qualname__函数对 pickle 有意想不到的影响.通过运行更多测试,我发现在对函数进行 pickle 时,pickle不像我想的那样工作,改变 __q
为什么 pickle 重用现有的 Python 类“C”而不是从 pickle 字节重建类?有没有一种方法可以在没有副作用的情况下 pickle 和解 pickle ? 这是我的回复 session
我使用 mpi4py 将一些计算拆分到多个过程中。基本上我只是计算一些凸包的体积,这是我使用 tvtk 和 mayavi 创建的。 只有第一个过程导入这些库: ... if rank==0: f
我正在用 pygame 制作一个绘图程序,我想在其中为用户提供一个选项来保存程序的确切状态,然后在稍后重新加载它。在这一点上,我保存了我的全局字典的副本,然后遍历, pickle 每个对象。 pyga
所以,我有一个对象,里面有很多不可 pickle 的东西(pygame 事件、orderedDicts、时钟等),我需要将它保存到磁盘。 事情是,如果我可以让这个东西存储一个有进度的字符串(我只需要一
import pickle variety = ["sweet", "box", "cat"] shape = ["back","spear", "log"] pickleFile = open("
我有一个关于 gensim 的问题。我想知道在保存或加载模型(或多个模型)时是否建议或需要使用 pickle,因为我在 GitHub 上找到了可以使用的脚本。 mymodel = Doc2Vec(do
我正在使用 python3.6/。我使用 protocol=pickle.HIGHEST_PROTOCOL pickle 了我的文件 当我按如下方式加载时: with open('data.sav',
给定一个像这样的任意Pythonic对象: class ExampleObj(object): def __init__(self): self.a = 'a'
简介 我有一本具有以下格式的字典: dict_list = {'S0':[[list of int],[list of int]], 'S1':[[list of int],[list of int]
我想知道这个错误可能意味着什么: PicklingError: Can't pickle : attribute lookup __builtin__.function failed 我理解这与使用多
我对 python 变量持久性有点困惑,在我的代码中,我使用以下代码使模型参数在某些迭代期间持久化 with open('W_Hs_Hu_iter'+str(inx)+'.pickle', 'wb'
当对象通过其属性之一引用自身时,从带有插槽的类中挑选对象的正确方法是什么?这是一个简单的示例,使用我当前的实现,我不确定它是否 100% 正确: import weakref import pickl
我有数千个长 (8640) 整数列表元组。例如: type(l1) tuple len(l1) 2 l1[0][:10] [0, 31, 23, 0, 0, 0, 0, 0, 0, 0] l1[1][
我有一个对象 gui_project,它有一个属性 .namespace,这是一个命名空间字典。 (即从字符串到对象的字典。) (这在类似 IDE 的程序中使用,让用户在 Python shell 中
我是一名优秀的程序员,十分优秀!