本文整理汇总了Python中zmq.utils.jsonapi.jsonmod.dumps函数的典型用法代码示例。如果您正苦于以下问题:Python dumps函数的具体用法?Python dumps怎么用?Python dumps使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了dumps函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: notify_event
def notify_event(self, topic, msg):
"""Publish a message on the event publisher channel"""
json_msg = json.dumps(msg)
if isinstance(json_msg, unicode):
json_msg = json_msg.encode('utf8')
if isinstance(self.res_name, unicode):
name = self.res_name.encode('utf8')
else:
name = self.res_name
multipart_msg = ["watcher.%s.%s" % (name, topic), json.dumps(msg)]
if self.evpub_socket is not None and not self.evpub_socket.closed:
self.evpub_socket.send_multipart(multipart_msg)
示例2: handle_autodiscover_message
def handle_autodiscover_message(self, fd_no, type):
data, address = self.udp_socket.recvfrom(1024)
data = json.loads(data)
self.udp_socket.sendto(
json.dumps({
'endpoint': self.endpoint
}), address)
示例3: send_msg
def send_msg(self, topic, msg):
"""send msg"""
json_msg = json.dumps(msg)
if isinstance(json_msg, unicode):
json_msg = json_msg.encode("utf8")
if isinstance(self.res_name, unicode):
name = self.res_name.encode("utf8")
else:
name = self.res_name
multipart_msg = ["watcher.%s.%s" % (name, topic), json.dumps(msg)]
if not self.evpub_socket.closed:
self.evpub_socket.send_multipart(multipart_msg)
示例4: cast
def cast(self, command, **props):
"""Fire-and-forget a command to **circusd**
Options:
- **command** -- the command to call
- **props** -- keyword arguments to add to the call
"""
msg = cast_message(command, **props)
self.client.send(json.dumps(msg))
示例5: call
def call(self, cmd):
if not isinstance(cmd, string_types):
try:
cmd = json.dumps(cmd)
except ValueError as e:
raise CallError(str(e))
try:
self.socket.send(cmd)
except zmq.ZMQError, e:
raise CallError(str(e))
示例6: call
def call(self, command, **props):
"""Sends the command to **circusd**
Options:
- **command** -- the command to call
- **props** -- keyword arguments to add to the call
Returns the JSON mapping sent back by **circusd**
"""
msg = make_message(command, **props)
self.client.send(json.dumps(msg))
msg = self.client.recv()
return json.loads(msg)
示例7: call
def call(self, cmd):
if isinstance(cmd, string_types):
raise DeprecationWarning('call() takes a mapping')
call_id = uuid.uuid4().hex
cmd['id'] = call_id
try:
cmd = json.dumps(cmd)
except ValueError as e:
raise CallError(str(e))
try:
self.socket.send(cmd)
except zmq.ZMQError, e:
raise CallError(str(e))
示例8: send_response
def send_response(self, cid, msg, resp):
if cid is None:
return
if not isinstance(resp, string_types):
resp = json.dumps(resp)
if isinstance(resp, unicode):
resp = resp.encode('utf8')
try:
self.stream.send(cid, zmq.SNDMORE)
self.stream.send(resp)
except zmq.ZMQError as e:
logger.debug("Received %r - Could not send back %r - %s", msg,
resp, str(e))
示例9: log
def log(self, entry):
"""Log an entry.
Will check if the handler contains filters and apply them if needed
(so that if multiple handlers have the same filters, they are only
applied once). Likewise, it will check if the log entry needs to be
serialized to JSON data, and make sure that the data is only
serialized once for each set of filters.
Parameters
----------
entry : dict
A log entry is a (JSON-compatible) dict.
"""
# Store entry for retrieval
self._entries.append(entry)
# For each set of filters, we store the JSON serialized entry
filtered_entries = {}
serialized_entries = {}
for handler in self.handlers:
filters = frozenset(handler.filters)
# If the content needs to be filtered, do so
if filters:
if filters not in filtered_entries:
filtered_entries[filters] = handler.filter(entry)
filtered_entry = filtered_entries[filters]
else:
filtered_entry = entry
# If the handler wants JSON data, give it
if handler.JSON:
if filters not in serialized_entries:
serialized_entries[filters] = json.dumps(
filtered_entry, **self.json_kwargs)
serialized_entry = serialized_entries[filters]
handler.log(serialized_entry)
else:
handler.log(filtered_entry)
示例10: send_response
def send_response(self, mid, cid, msg, resp, cast=False):
if cast:
return
if cid is None:
return
if isinstance(resp, string_types):
raise DeprecationWarning('Takes only a mapping')
resp['id'] = mid
resp = json.dumps(resp)
if isinstance(resp, unicode):
resp = resp.encode('utf8')
try:
self.stream.send(cid, zmq.SNDMORE)
self.stream.send(resp)
except zmq.ZMQError as e:
logger.debug("Received %r - Could not send back %r - %s", msg,
resp, str(e))
示例11: call
def call(self, cmd, callback):
if not isinstance(cmd, string_types):
try:
cmd = json.dumps(cmd)
except ValueError as e:
raise CallError(str(e))
socket = self.context.socket(zmq.DEALER)
socket.setsockopt(zmq.IDENTITY, uuid.uuid4().hex)
socket.setsockopt(zmq.LINGER, 0)
get_connection(socket, self.endpoint, self.ssh_server,
self.ssh_keyfile)
if callback:
stream = ZMQStream(socket, self.loop)
def timeout_callback():
stream.stop_on_recv()
stream.close()
raise CallError('Call timeout for cmd', cmd)
timeout = self.loop.add_timeout(timedelta(seconds=5),
timeout_callback)
def recv_callback(msg):
self.loop.remove_timeout(timeout)
stream.stop_on_recv()
stream.close()
callback(json.loads(msg[0]))
stream.on_recv(recv_callback)
try:
socket.send(cmd)
except zmq.ZMQError as e:
raise CallError(str(e))
if not callback:
return json.loads(socket.recv())
示例12: make_json
def make_json(command, **props):
return json.dumps(make_message(command, **props))
示例13: call
def call(self, cmd):
self.client.send(json.dumps(cmd))
msg = self.client.recv()
return json.loads(msg)
示例14: cast
def cast(self, cmd):
self.client.send(json.dumps(cmd))
示例15: cast
def cast(self, command, **props):
msg = cast_message(command, **props)
self.client.send(json.dumps(msg))