diff options
author | Pablo Galindo <Pablogsal@gmail.com> | 2017-09-07 20:53:13 (GMT) |
---|---|---|
committer | Christian Heimes <christian@python.org> | 2017-09-07 20:53:13 (GMT) |
commit | 586c0502b5eb9a39cabe0bc2707a8ff63114265c (patch) | |
tree | 647486247ca97afacf4e562a116c785fd6083f75 /Doc/howto/logging-cookbook.rst | |
parent | 397c467c49385023de36411194d381ac993bae1a (diff) | |
download | cpython-586c0502b5eb9a39cabe0bc2707a8ff63114265c.zip cpython-586c0502b5eb9a39cabe0bc2707a8ff63114265c.tar.gz cpython-586c0502b5eb9a39cabe0bc2707a8ff63114265c.tar.bz2 |
bpo-31294: Fix ZeroMQSocketListener and ZeroMQSocketHandler examples (#3229)
* Fix ZeroMQSocketListener and ZeroMQSocketHandler examples
* Use send_json and recv_json to simplify pyzmq interfacing
* Add News entry
Diffstat (limited to 'Doc/howto/logging-cookbook.rst')
-rw-r--r-- | Doc/howto/logging-cookbook.rst | 16 |
1 files changed, 8 insertions, 8 deletions
diff --git a/Doc/howto/logging-cookbook.rst b/Doc/howto/logging-cookbook.rst index 24cf76b..71ee417 100644 --- a/Doc/howto/logging-cookbook.rst +++ b/Doc/howto/logging-cookbook.rst @@ -1258,8 +1258,8 @@ socket is created separately and passed to the handler (as its 'queue'):: class ZeroMQSocketHandler(QueueHandler): def enqueue(self, record): - data = json.dumps(record.__dict__) - self.queue.send(data) + self.queue.send_json(record.__dict__) + handler = ZeroMQSocketHandler(sock) @@ -1272,11 +1272,10 @@ data needed by the handler to create the socket:: self.ctx = ctx or zmq.Context() socket = zmq.Socket(self.ctx, socktype) socket.bind(uri) - QueueHandler.__init__(self, socket) + super().__init__(socket) def enqueue(self, record): - data = json.dumps(record.__dict__) - self.queue.send(data) + self.queue.send_json(record.__dict__) def close(self): self.queue.close() @@ -1292,12 +1291,13 @@ of queues, for example a ZeroMQ 'subscribe' socket. Here's an example:: def __init__(self, uri, *handlers, **kwargs): self.ctx = kwargs.get('ctx') or zmq.Context() socket = zmq.Socket(self.ctx, zmq.SUB) - socket.setsockopt(zmq.SUBSCRIBE, '') # subscribe to everything + socket.setsockopt_string(zmq.SUBSCRIBE, '') # subscribe to everything socket.connect(uri) + super().__init__(socket, *handlers, **kwargs) def dequeue(self): - msg = self.queue.recv() - return logging.makeLogRecord(json.loads(msg)) + msg = self.queue.recv_json() + return logging.makeLogRecord(msg) .. seealso:: |