Skip to content

Commit 586c050

Browse files
pablogsaltiran
authored andcommitted
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
1 parent 397c467 commit 586c050

File tree

3 files changed

+11
-8
lines changed

3 files changed

+11
-8
lines changed

Doc/howto/logging-cookbook.rst

+8-8
Original file line numberDiff line numberDiff line change
@@ -1258,8 +1258,8 @@ socket is created separately and passed to the handler (as its 'queue')::
12581258

12591259
class ZeroMQSocketHandler(QueueHandler):
12601260
def enqueue(self, record):
1261-
data = json.dumps(record.__dict__)
1262-
self.queue.send(data)
1261+
self.queue.send_json(record.__dict__)
1262+
12631263

12641264
handler = ZeroMQSocketHandler(sock)
12651265

@@ -1272,11 +1272,10 @@ data needed by the handler to create the socket::
12721272
self.ctx = ctx or zmq.Context()
12731273
socket = zmq.Socket(self.ctx, socktype)
12741274
socket.bind(uri)
1275-
QueueHandler.__init__(self, socket)
1275+
super().__init__(socket)
12761276

12771277
def enqueue(self, record):
1278-
data = json.dumps(record.__dict__)
1279-
self.queue.send(data)
1278+
self.queue.send_json(record.__dict__)
12801279

12811280
def close(self):
12821281
self.queue.close()
@@ -1292,12 +1291,13 @@ of queues, for example a ZeroMQ 'subscribe' socket. Here's an example::
12921291
def __init__(self, uri, *handlers, **kwargs):
12931292
self.ctx = kwargs.get('ctx') or zmq.Context()
12941293
socket = zmq.Socket(self.ctx, zmq.SUB)
1295-
socket.setsockopt(zmq.SUBSCRIBE, '') # subscribe to everything
1294+
socket.setsockopt_string(zmq.SUBSCRIBE, '') # subscribe to everything
12961295
socket.connect(uri)
1296+
super().__init__(socket, *handlers, **kwargs)
12971297

12981298
def dequeue(self):
1299-
msg = self.queue.recv()
1300-
return logging.makeLogRecord(json.loads(msg))
1299+
msg = self.queue.recv_json()
1300+
return logging.makeLogRecord(msg)
13011301

13021302

13031303
.. seealso::

Misc/ACKS

+1
Original file line numberDiff line numberDiff line change
@@ -1561,6 +1561,7 @@ Martin Teichmann
15611561
Gustavo Temple
15621562
Mikhail Terekhov
15631563
Victor Terrón
1564+
Pablo Galindo
15641565
Richard M. Tew
15651566
Tobias Thelen
15661567
Christian Theune
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix incomplete code snippet in the ZeroMQSocketListener and
2+
ZeroMQSocketHandler examples and adapt them to Python 3.

0 commit comments

Comments
 (0)