zeromq and bind_to_random_port - how to get port chosen
Asked Answered
P

1

11

In python, I am using the following:

    context = zmq.Context()
    socket = context.socket(zmq.PUSH)
    socket.bind_to_random_port('tcp://*', min_port=6001, max_port=6004, max_tries=100)
    port_selected = socket.???????

How do I know what port is chosen? I will have a look up table in redis for the workers to read.

I am using a push pull model. I need to let workers know what ports to connect to.

I have to do this because I am using the gevent loop in uwsgi and specifying a a plain blind thows and error becuase of a fork. If a use bind_to_random_port then a port is seleced, I just dont know which.

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/gevent-1.0b2-py2.7-linux-x86_64.egg/gevent/greenlet.py",
line 328, in run
    result = self._run(*self.args, **self.kwargs)
  File "/home/ubuntu/workspace/rtbopsConfig/rtbServers/rtbUwsgiPixelServer/uwsgiPixelServer.py",
line 43, in sendthis
    socket.send(push)
  File "/usr/local/lib/python2.7/dist-packages/zmq/green/core.py",
line 173, in send
    self._wait_write()
  File "/usr/local/lib/python2.7/dist-packages/zmq/green/core.py",
line 108, in _wait_write
    assert self.__writable.ready(), "Only one greenlet can be waiting
on this event"
AssertionError: Only one greenlet can be waiting on this event
<Greenlet at 0x2d41370: sendthis('2012-07-02 04:05:15')> failed with
AssertionError
Pseudoscope answered 5/7, 2012 at 9:48 Comment(0)
M
18
port_selected = socket.bind_to_random_port('tcp://*', min_port=6001, max_port=6004, max_tries=100)
Mccullough answered 5/7, 2012 at 18:3 Comment(2)
reference: zeromq.github.com/pyzmq/api/…Regional
How you tell the client socket about the port chosen?Orgy

© 2022 - 2024 — McMap. All rights reserved.