From 06598f7dccf1c20a1126fbed15a6546d9e9289c7 Mon Sep 17 00:00:00 2001 From: msincenselee Date: Sat, 17 Jun 2017 19:59:59 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0encode/decode=EF=BC=8C?= =?UTF-8?q?=E6=94=AF=E6=8C=81unicode=E7=9A=84event?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vnpy/rpc/vnrpc.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/vnpy/rpc/vnrpc.py b/vnpy/rpc/vnrpc.py index c3a979c9..e9325743 100644 --- a/vnpy/rpc/vnrpc.py +++ b/vnpy/rpc/vnrpc.py @@ -38,8 +38,8 @@ class RpcObject(object): def __init__(self): """Constructor""" # 默认使用msgpack作为序列化工具 - #self.useMsgpack() - self.usePickle() + self.useMsgpack() + #self.usePickle() #---------------------------------------------------------------------- def pack(self, data): @@ -84,18 +84,22 @@ class RpcObject(object): #---------------------------------------------------------------------- def useJson(self): """使用json作为序列化工具""" + print 'Use Json Serialization' + self.pack = self.__jsonPack self.unpack = self.__jsonUnpack #---------------------------------------------------------------------- def useMsgpack(self): """使用msgpack作为序列化工具""" + print 'Use MsgPack Serialization' self.pack = self.__msgpackPack self.unpack = self.__msgpackUnpack #---------------------------------------------------------------------- def usePickle(self): """使用cPickle作为序列化工具""" + print 'Use Pickle Serialization' self.pack = self.__picklePack self.unpack = self.__pickleUnpack @@ -185,7 +189,7 @@ class RpcServer(RpcObject): """ # 序列化数据 datab = self.pack(data) - + topic = topic.encode('utf-8') # 通过广播socket发送数据 self.__socketPUB.send_multipart([topic, datab]) @@ -194,7 +198,6 @@ class RpcServer(RpcObject): """注册函数""" self.__functions[func.__name__] = func - ######################################################################## class RpcClient(RpcObject): """RPC客户端""" @@ -276,7 +279,8 @@ class RpcClient(RpcObject): # 从订阅socket收取广播数据 topic, datab = self.__socketSUB.recv_multipart() - + topic = topic.decode("utf-8") + # 序列化解包 data = self.unpack(datab) @@ -295,9 +299,9 @@ class RpcClient(RpcObject): 可以使用topic=''来订阅所有的主题 """ + topic = topic.encode('utf-8') self.__socketSUB.setsockopt(zmq.SUBSCRIBE, topic) - - + ######################################################################## class RemoteException(Exception):