I'm trying to get a PUB-SUB communication in ZeroMQ working where the PUB is in C++ and the SUB in python. I am using python 3.8, ZeroMQ 4.3.2, pyzmq 18.1.1 and cppzmq 4.5.0
The PUB :
int main()
{
Sleep(10000);
zmq::context_t context(1);
zmq::socket_t publisher(context, ZMQ_PUB);
publisher.bind("tcp://*:5556");
int zipcode, temperature, relhumidity;
while (true) {
// Get values (first supposed to be random)
zipcode = 10001;
temperature = 27;
relhumidity = 61;
// Send message to the subscriber
zmq::message_t message(20);
snprintf((char *)message.data(), 20, "%05d %d %d", zipcode, temperature, relhumidity);
publisher.send(message, zmq::send_flags::none);
std::fprintf(stderr, "[INFO] Sent data: %i, %i, %i \n", zipcode, temperature, relhumidity);
if (fValue && j >= fValue) {
break;
}
j++;
}
}
The SUB :
import sys
import zmq
# Socket to talk to server
context = zmq.Context()
socket = context.socket(zmq.SUB)
socket.connect("tcp://localhost:5556")
# Subscribe to zipcode, default is NYC, 10001
zip_filter = "10001"
# Python 2 - ascii bytes to unicode str
if isinstance(zip_filter, bytes):
zip_filter = zip_filter.decode('ascii')
socket.setsockopt_string(zmq.SUBSCRIBE, zip_filter)
# Process 5 updates
for update_nbr in range(3):
string = socket.recv_string()
zipcode, temperature, relhumidity = string.split()
print("Received data : %s , %d , %d" % (zip_filter, temperature, relhumidity))
But I can't get it to work as the SUB wait for ever on string = socket.recv_string()
whereas the PUB sends messages without errors. Indeed, it returns the length of the sent messages.
Notes :
- the Sleep is for trial, to be able to launch the SUB before the PUB sends. But if I remove it and launch the SUB first, I have the same behavior.
- If I do the following, it prints
none
:
Err = socket.connect("tcp://localhost:5556")
print(Err)
I am new to ZeroMQ and I don't really know where to start to solve this. Any idea ?