[zeromq-dev] PUB/SUB not sending data

Francesco francesco.montorsi at gmail.com
Wed Nov 22 14:38:11 CET 2023

Hi Brett, Hi Raul,

My feeling is that there is a misconception as well, but that's related to
the socket lifetime.
A PUB socket is not an object meant to be created, have 1 msg pushed out
and then immediately destroyed, as it happens inside the senderzmq() python
function of original email.
The point is that the PUB socket creates a zmq "server" endpoint and
* the SUB needs to be able to connect to it (this requires a TCP handshake
and more stuff happening transparently to the user in the ZMQ background
* the SUB needs to be able to subscribe to it
Only after these 2 things happen, the subscriber can truly receive data.
Creating a PUB socket and immediately destroying it means all these
operations need to be repeated. And if you remove the "while Running" from
senderzmq() what happens is that the PUB socket is created, you ask ZMQ to
accept ("bind") connections coming to it, then (at very high speed) you
send a message but no subscriber can realistically be already connected, so
it gets dropped.

Long story short:
* try putting a sleep after the bind() and the send_string() of about
500msec -- then your application should work even if you remove the "while
* a better design for your "sender" application is to create the PUB socket
once (as global variable or somewhere else) and then _reuse_ it
everytime you need to send out a latitude/longitude sample


Il giorno mer 22 nov 2023 alle ore 14:14 Brett Viren via zeromq-dev <
zeromq-dev at lists.zeromq.org> ha scritto:

> Hi Raúl,
> I feel that your questions may imply a deeper misunderstanding than what
> they ask directly so let me offer a few comments that hopefully gets to
> the real problems.  Feel free to ask more.
> Of course the "while True" loop in the sender will send only identical
> copies of the same message (and will do so at a very high rate).
> There is no "listen new data" feature that I can think of.
> Any code that calls the senderzmq() will find that this function never
> returns.
> It seems senderzmq() should be rewritten to simply omit the "while True"
> loop so that the caller may call it many times and each time with some
> new values of lat and lon.
> One concrete thing I notice in your code.  Your the SUB socket in the
> receiver does not register any topic.  See:
>   https://pyzmq.readthedocs.io/en/latest/api/zmq.html#zmq.Socket.subscribe
> Because of that, I expect the call to recv_string() never returns.
> -Brett.
> Raúl Parada Medina <raul.parada.medina at gmail.com> writes:
> > Hi,
> >
> > I'm sending data in python between two connect machines within the same
> network (successfully
> > connected).
> >
> > The publisher code is defined as:
> >
> > def senderzmq(self, lat, lon):
> >     sock = zmq.Context().socket(zmq.PUB)
> >     sock.bind("tcp://*:10000")
> >     running = True
> >     while running:
> >         sock.send_string(f"Lat {lat}, Lon={lon}")
> >
> > The receiver has the following format:
> >
> > import zmq
> > context=zmq.Context()
> > socket=context.socket(zmq.SUB)
> > socket.connect("tcp://")
> > while True:
> >          message=socket.recv_string()
> >          print(message)
> >
> > The above development works, however, the sender always sends the same
> data, it looks the sender
> > remains within the while True state not listening new data. If I remove
> the while True, the above
> > doesn't work. Any idea how to exit the while True but transmitting new
> data at each loop?
> >
> > Thanks.
> > Raúl
> > _______________________________________________
> > zeromq-dev mailing list
> > zeromq-dev at lists.zeromq.org
> >
> https://urldefense.com/v3/__https://lists.zeromq.org/mailman/listinfo/zeromq-dev__;!!P4SdNyxKAPE!FdPTbAQ8PU-86XJs4hElWe3bCxrehQdjDjBH4O0bBEi2pIuVDIcT8FEo2UiwWt8dgjvqYmU0y8Gy0XiNlliTOuM$
> _______________________________________________
> zeromq-dev mailing list
> zeromq-dev at lists.zeromq.org
> https://lists.zeromq.org/mailman/listinfo/zeromq-dev
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.zeromq.org/pipermail/zeromq-dev/attachments/20231122/c37fad0f/attachment.htm>

More information about the zeromq-dev mailing list