'How do I send a dict with the PUB/SUB pattern using pyzmq?
I'm trying to send a python dictionary, using something like:
import zmq
context = zmq.Context()
socket = context.socket(zmq.PUB)
socket.bind("tcp://*:%s" % port
socket.send_pyobj({'hello':'world'})
and receive it using:
socket.connect ("tcp://localhost:%s" % port)
topicfilter = "1"
socket.setsockopt_string(zmq.SUBSCRIBE, topicfilter)
while True:
print(socket.recv_pyobj())
Questions:
- I can't seem to figure out how to set the topic for
send_pyobj()
. - Needless to say, I don't receive anything on the other end. I managed to get it going with strings and
send_string()
, so it's definitely connecting. What am I doing wrong? - Can I have many
PUB
servers broadcasting? Thereby creating a sort of many-to-many, where other apps can dip into the flow?
Solution 1:[1]
- Can I have many
PUB
servers broadcasting?
Yes, you can
Easy.
- I don't receive anything on the other end... What am I doing wrong?
Do not subscribe to "1"
in cases, when it never comes
This single reason stands behind "not receiving anything" ( unless it started with "1"
, which it obviously never did, as the payload was assembled right by the socket.send_object( ... )
and there was never a "1"
to match on the position of the string-alike byte[0].
- ... how to set the topic for
send_pyobj()
?
Well, you never know
This ought be set using other means than guessing a few bytes, "matching" the initial section of the object-byte-string-representation.
Simplest ever is to "prepend" a controlled value on the sender side like this :
import dill as pickle
aConstantNbytesPUB_TOPIC_FILTER_PREFIX = "{UUID:0000-0000-0000-1111-2222}"
socket.send(aConstantNbytesPUB_TOPIC_FILTER_PREFIX + pickle.dumps(anObjectToSEND))
#...
aMSG = socket.recv()
print(pickle.loads(aMSG[len(aConstantNbytesPUB_TOPIC_FILTER_PREFIX) :]))
Using a multi-part message composition is also possible, yet a bit longer to demonstrate the concept here.
Both pickle
and dill
are limited in their SER/DES-strategies for object decomposition / traversal / reinstatement capabilities and security warnings are well known to be taken care off.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | pdoherty926 |