5

This question has been asked before, here. I have the exact same problem. I want to publish from a bunch of different processes, and use the same port every time.

I tried the solution presented in the answer, but this did not work for me. I get the error

    File "/usr/local/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap
    self.run()
  File "/usr/local/lib/python2.7/multiprocessing/process.py", line 114, in run
    self._target(*self._args, **self._kwargs)
  File "/home/akay/afk/multi.py", line 18, in to_zmq
    socket.connect("tcp://*:%s" % port)
  File "zmq/backend/cython/socket.pyx", line 478, in zmq.backend.cython.socket.Socket.connect (zmq/backend/cython/socket.c:4308)
ZMQError: Invalid argument

My code is like this, essentially taken straight from the example in the zmq docs here and here:

# Socket to talk to server
port = '5556'
context = zmq.Context()
socket = context.socket(zmq.SUB)
print "Listening for stream...", m
socket.bind("tcp://localhost:%s" % port) #change connect to bind, as per answer above
socket.setsockopt(zmq.SUBSCRIBE, topicfilter)

I am using python 2.7, and the most recent version of zmq. Any idea what I might be doing wrong?

Community
  • 1
  • 1
Wapiti
  • 1,851
  • 2
  • 20
  • 40
  • add an identifier (which works as a virtual port) in the content of the socket message to identify each different process by a UUID (for example) – Nikos M. May 31 '15 at 01:24
  • Thank you. Can you be more specific about the content of the socket message? I just started using zmq and am still getting my head around it. – Wapiti May 31 '15 at 01:26
  • hmm i'm trying to understand the actual problem, is it your software problme, i.e how identify different clients on same socket or a netwrok problem, re-use same socket/port connection? My comment refrers to 1st case – Nikos M. May 31 '15 at 01:27
  • I'm talking about the 2nd case. – Wapiti May 31 '15 at 01:28
  • 1
    It looks like you're getting the error on the publisher, but you showed us the code for the subscriber. Show the code for the publisher. – Jason Jun 01 '15 at 13:30

1 Answers1

3

Well, the error is clear:

    [...]
    socket.connect("tcp://*:%s" % port)
    [...]
ZMQError: Invalid argument

You can't connect to *, you must specify an IP address (the server IP address). If both the client and the server run on a single machine, try with localhost or 127.0.0.1.

Peque
  • 13,638
  • 11
  • 69
  • 105
  • I'm accepting this answer as the question is old with the caveat that I have not verified the solution for myself (I no longer have easy access to the code, having solved the problem another way). – Wapiti Jul 20 '15 at 18:33
  • 1
    This answer probably should not have been accepted. See the example code here http://learning-0mq-with-pyzmq.readthedocs.org/en/latest/pyzmq/patterns/pubsub.html – Wapiti Oct 08 '15 at 12:17
  • @Wapiti: could you please explain why it should not have been accepted? My answer is correct and it shows why you were getting the `Invalid argument` error. – Peque Oct 08 '15 at 12:21
  • You might be right about this -- the link in the original question says to switch bind and connect. Are you saying the problem is that when you do that you also need to switch the arguments, (that's what I didn't do in my code). So, instead of `socked.bind(''tcp://*:%s' %port` as in the example I linked in the comment above, you have `socked.connect(''tcp://localhost:%s' %port` ? – Wapiti Oct 08 '15 at 19:23
  • @Wapiti: what I'm saying is that your question was about a specific error you were getting: `ZMQError: Invalid argument` (that was your problem at the moment). That error is due to your code trying to connect to `*`, but you cannot conntect to `*`; you need to specify an IP address or, otherwise, use `localhost`. If you do so, then you don't get the error you posted in the question. If, after that, you still have other problems, then you should ask a new question explaining your new problems and with your updated code. ;-) – Peque Oct 08 '15 at 19:38
  • OK, I reaccepted. I thought your answer was wrong because the example code has `*` in the argument of `bind`. But you were suggesting this is wrong when calling `connect`. – Wapiti Oct 08 '15 at 20:33
  • @Wapiti: exactly. :-) – Peque Oct 09 '15 at 08:53