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:
send_pyobj()
.send_string()
, so it's definitely connecting. What am I doing wrong?PUB
servers broadcasting? Thereby creating a sort of many-to-many, where other apps can dip into the flow?
- Can I have many
PUB
servers broadcasting?
Easy.
- I don't receive anything on the other end... What am I doing wrong?
"1"
in cases, when it never comesThis 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()
?
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.