mirror of
https://github.com/python/cpython.git
synced 2025-10-01 12:52:18 +00:00
[3.6] bpo-31294: Fix ZeroMQSocketListener and ZeroMQSocketHandler examples (GH-3229) (#3430)
* Fix ZeroMQSocketListener and ZeroMQSocketHandler examples
* Use send_json and recv_json to simplify pyzmq interfacing
* Add News entry
(cherry picked from commit 586c0502b5
)
This commit is contained in:
parent
e89b35dd2b
commit
27ce5a1b19
3 changed files with 11 additions and 8 deletions
|
@ -1258,8 +1258,8 @@ socket is created separately and passed to the handler (as its 'queue')::
|
||||||
|
|
||||||
class ZeroMQSocketHandler(QueueHandler):
|
class ZeroMQSocketHandler(QueueHandler):
|
||||||
def enqueue(self, record):
|
def enqueue(self, record):
|
||||||
data = json.dumps(record.__dict__)
|
self.queue.send_json(record.__dict__)
|
||||||
self.queue.send(data)
|
|
||||||
|
|
||||||
handler = ZeroMQSocketHandler(sock)
|
handler = ZeroMQSocketHandler(sock)
|
||||||
|
|
||||||
|
@ -1272,11 +1272,10 @@ data needed by the handler to create the socket::
|
||||||
self.ctx = ctx or zmq.Context()
|
self.ctx = ctx or zmq.Context()
|
||||||
socket = zmq.Socket(self.ctx, socktype)
|
socket = zmq.Socket(self.ctx, socktype)
|
||||||
socket.bind(uri)
|
socket.bind(uri)
|
||||||
QueueHandler.__init__(self, socket)
|
super().__init__(socket)
|
||||||
|
|
||||||
def enqueue(self, record):
|
def enqueue(self, record):
|
||||||
data = json.dumps(record.__dict__)
|
self.queue.send_json(record.__dict__)
|
||||||
self.queue.send(data)
|
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
self.queue.close()
|
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):
|
def __init__(self, uri, *handlers, **kwargs):
|
||||||
self.ctx = kwargs.get('ctx') or zmq.Context()
|
self.ctx = kwargs.get('ctx') or zmq.Context()
|
||||||
socket = zmq.Socket(self.ctx, zmq.SUB)
|
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)
|
socket.connect(uri)
|
||||||
|
super().__init__(socket, *handlers, **kwargs)
|
||||||
|
|
||||||
def dequeue(self):
|
def dequeue(self):
|
||||||
msg = self.queue.recv()
|
msg = self.queue.recv_json()
|
||||||
return logging.makeLogRecord(json.loads(msg))
|
return logging.makeLogRecord(msg)
|
||||||
|
|
||||||
|
|
||||||
.. seealso::
|
.. seealso::
|
||||||
|
|
|
@ -1537,6 +1537,7 @@ Martin Teichmann
|
||||||
Gustavo Temple
|
Gustavo Temple
|
||||||
Mikhail Terekhov
|
Mikhail Terekhov
|
||||||
Victor Terrón
|
Victor Terrón
|
||||||
|
Pablo Galindo
|
||||||
Richard M. Tew
|
Richard M. Tew
|
||||||
Tobias Thelen
|
Tobias Thelen
|
||||||
Févry Thibault
|
Févry Thibault
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
Fix incomplete code snippet in the ZeroMQSocketListener and
|
||||||
|
ZeroMQSocketHandler examples and adapt them to Python 3.
|
Loading…
Add table
Add a link
Reference in a new issue