Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

zeromq subscribe and publish simultaneously

I am attempting to use zeromq in a debian/multiprocess situation with python. I have several processes decoding raw data from a multitude of sensors, and publishing in zmq. So far so good. I have a computation process which subscribes to the raw data on zmq, and performs some computations. It then needs to publish its answer for other processes to log and consume. Hence my computation process needs to both subscribe AND publish in the same process.

import zmq
import json
context = zmq.Context()

sub = context.socket(zmq.SUB)
sub.setsockopt(zmq.SUBSCRIBE, '')
sub.setsockopt(zmq.RCVBUF, 0)
sub.bind("tcp://127.0.0.1:5000")

pub = context.socket(zmq.PUB)
pub.bind("tcp://127.0.0.1:5000")

I tried the above, but no luck. It returns an err...

File "socket.pyx", line 465, in zmq.core.socket.Socket.bind (zmq/core/socket.c:4749)
zmq.core.error.ZMQError: Address already in use

I read the documentation, google etc, but am now completely stuck.

like image 569
pktrigg Avatar asked Nov 04 '22 15:11

pktrigg


1 Answers

You are trying to bind to the same port (5000) twice which gives you the Address already in use error - you can only bind one socket to a port.

But the sub socket shouldn't bind(), it should do a connect() to some host/port that another process is publishing to (i.e. has done bind() on). The code should look something like:

sub = context.socket(zmq.SUB)
sub.setsockopt(zmq.SUBSCRIBE, '')
sub.setsockopt(zmq.RCVBUF, 0)
sub.connect("tcp://127.0.0.1:XXXX")

pub = context.socket(zmq.PUB)
pub.bind("tcp://127.0.0.1:YYYY")

I'm guessing that you don't mean to have ports XXXX and YYYY the same, then the same process would listen to the messages it is itself publishing.

like image 154
johlo Avatar answered Nov 10 '22 21:11

johlo