def send(self, topic, payload):
'''Send a message with topic, payload
`
Topic is a unicode string. It will be sent as utf-8 encoded byte array.
Payload is a python dict. It will be sent as a msgpack serialized dict.
If payload has the key '__raw_data__'
we pop if of the payload and send its raw contents as extra frames
everything else need to be serializable
the contents of the iterable in '__raw_data__'
require exposing the pyhton memoryview interface.
'''
if '__raw_data__' not in payload:
self.socket.send_string(topic, flags=zmq.SNDMORE)
self.socket.send(serializer.dumps(payload, use_bin_type=True))
else:
extra_frames = payload.pop('__raw_data__')
assert(isinstance(extra_frames, (list, tuple)))
self.socket.send_string(topic, flags=zmq.SNDMORE)
self.socket.send(serializer.dumps(payload), flags=zmq.SNDMORE)
for frame in extra_frames[:-1]:
self.socket.send(frame, flags=zmq.SNDMORE, copy=True)
self.socket.send(extra_frames[-1], copy=True)
评论列表
文章目录