本文整理汇总了Python中msgpack_numpy.encode方法的典型用法代码示例。如果您正苦于以下问题:Python msgpack_numpy.encode方法的具体用法?Python msgpack_numpy.encode怎么用?Python msgpack_numpy.encode使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类msgpack_numpy
的用法示例。
在下文中一共展示了msgpack_numpy.encode方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: set_subscriber_topic
# 需要导入模块: import msgpack_numpy [as 别名]
# 或者: from msgpack_numpy import encode [as 别名]
def set_subscriber_topic(self, topic, subscriber_socket):
"""
This method sets a subscriber topic.
You can subscribe to multiple topics by calling this method for
each topic.
:param topic: A topic string
:param subscriber_socket: subscriber socket
:return:
"""
# make sure topic is a string
if not type(topic) is str:
raise TypeError('Subscriber topic must be python_banyan string')
# does the subscriber socket exist?
if subscriber_socket:
subscriber_socket.setsockopt(zmq.SUBSCRIBE, topic.encode())
else:
raise ValueError('set_subscriber_topic: socket is None')
示例2: unsubscribe_topic
# 需要导入模块: import msgpack_numpy [as 别名]
# 或者: from msgpack_numpy import encode [as 别名]
def unsubscribe_topic(self, topic, subscriber_socket):
"""
This method un-subscribes from a topic.
:param topic: A topic string
:param subscriber_socket: subscriber socket
:return:
"""
# make sure topic is a string
if not type(topic) is str:
raise TypeError('Subscriber topic must be python_banyan string')
# make sure that a socket reference has been passed in
if subscriber_socket:
subscriber_socket.unsubscribe(topic.encode())
else:
raise ValueError('set_subscriber_topic: socket is None')
示例3: publish_payload
# 需要导入模块: import msgpack_numpy [as 别名]
# 或者: from msgpack_numpy import encode [as 别名]
def publish_payload(self, payload, topic=''):
"""
This method will publish a python_banyan payload and its associated topic
:param payload: Protocol message to be published
:param topic: A string value
"""
# make sure the topic is a string
if not type(topic) is str:
raise TypeError('Publish topic must be python_banyan string', 'topic')
if self.numpy:
message = await self.numpy_pack(payload)
else:
message = await self.pack(payload)
pub_envelope = topic.encode()
await self.publisher.send_multipart([pub_envelope, message])
# await asyncio.sleep(1)
示例4: publish_payload
# 需要导入模块: import msgpack_numpy [as 别名]
# 或者: from msgpack_numpy import encode [as 别名]
def publish_payload(self, payload, topic=''):
"""
This method will publish a python_banyan payload and its associated topic
:param payload: Protocol message to be published
:param topic: A string value
"""
# make sure the topic is a string
if not type(topic) is str:
raise TypeError('Publish topic must be python_banyan string', 'topic')
# create python_banyan message pack payload
if self.numpy:
message = msgpack.packb(payload, default=m.encode)
else:
message = msgpack.packb(payload, use_bin_type=True)
pub_envelope = topic.encode()
self.publisher.send_multipart([pub_envelope, message])
示例5: _generate_response_channel
# 需要导入模块: import msgpack_numpy [as 别名]
# 或者: from msgpack_numpy import encode [as 别名]
def _generate_response_channel(self):
random_hash = hashlib.md5("{}".format(random.randint(0, 10**10)).encode('utf-8')).hexdigest()
response_channel = "{}::{}::response::{}".format( self.namespace,
self.service_id,
random_hash)
return response_channel
示例6: _blocking_request
# 需要导入模块: import msgpack_numpy [as 别名]
# 或者: from msgpack_numpy import encode [as 别名]
def _blocking_request(self, _request):
"""
request:
-command_type
-payload
-response_channel
response: (on response_channel)
- RESULT
* Send the payload on command_channel (self.namespace+"::command")
** redis-left-push (LPUSH)
* Keep listening on response_channel (BLPOP)
"""
assert type(_request) ==type({})
_request['response_channel'] = self._generate_response_channel()
_redis = self.get_redis_connection()
"""
The client always pushes in the left
and the service always pushes in the right
"""
if self.verbose: print("Request : ", _response)
# Push request in command_channels
payload = msgpack.packb(_request, default=m.encode, use_bin_type=True)
_redis.lpush(self.command_channel, payload)
## TODO: Check if we can use `repr` for json.dumps string serialization
# Wait with a blocking pop for the response
_response = _redis.blpop(_request['response_channel'])[1]
if self.verbose: print("Response : ", _response)
_response = msgpack.unpackb(_response, object_hook=m.decode, encoding="utf8")
if _response['type'] == messages.OSIM_RL.ERROR:
raise Exception(str(_response))
else:
return _response
示例7: publish_payload
# 需要导入模块: import msgpack_numpy [as 别名]
# 或者: from msgpack_numpy import encode [as 别名]
def publish_payload(self, payload, publisher_socket, topic=''):
"""
This method will publish a python_banyan payload and its associated topic
:param payload: Protocol message to be published
:param publisher_socket: Publisher socket - handle to socket or "BROADCAST" to send to
all connected publisher sockets
:param topic: A string value for message topic
:return:
"""
# make sure topic is a string
if not type(topic) is str:
raise TypeError('Publish topic must be python_banyan string', 'topic')
# create python_banyan message pack payload
if self.numpy:
message = msgpack.packb(payload, default=m.encode)
else:
message = umsgpack.packb(payload)
pub_envelope = topic.encode()
if publisher_socket == "BROADCAST":
for element in self.backplane_table:
if element['publisher']:
element['publisher'].send_multipart([pub_envelope, message])
else:
if publisher_socket:
publisher_socket.send_multipart([pub_envelope, message])
else:
raise ValueError('Invalid publisher socket')
示例8: numpy_pack
# 需要导入模块: import msgpack_numpy [as 别名]
# 或者: from msgpack_numpy import encode [as 别名]
def numpy_pack(self, data):
return msgpack.packb(data, default=m.encode)
示例9: set_subscriber_topic
# 需要导入模块: import msgpack_numpy [as 别名]
# 或者: from msgpack_numpy import encode [as 别名]
def set_subscriber_topic(self, topic):
"""
This method sets a subscriber topic.
You can subscribe to multiple topics by calling this method for
each topic.
:param topic: A topic string
"""
if not type(topic) is str:
raise TypeError('Subscriber topic must be python_banyan string')
self.subscriber.setsockopt(zmq.SUBSCRIBE, topic.encode())
示例10: np_serialize
# 需要导入模块: import msgpack_numpy [as 别名]
# 或者: from msgpack_numpy import encode [as 别名]
def np_serialize(x):
import msgpack
import msgpack_numpy as m
return msgpack.packb(np.array(x, 'float32'), default=m.encode)
#return msgpack.packb(x, default=m.encode)
# f = StringIO.StringIO()
# np.save(f, x)
# return f.getvalue()
示例11: serialize
# 需要导入模块: import msgpack_numpy [as 别名]
# 或者: from msgpack_numpy import encode [as 别名]
def serialize(vector):
""" Serializer a vector using msgpack.
:param np.array vector:
:return bytes:
"""
return msgpack.packb(vector, default = msgpack_numpy.encode)