Python zmq.LINGER Examples

The following are 30 code examples of zmq.LINGER(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module zmq , or try the search function .
Example #1
Source File: remote.py    From ternarynet with Apache License 2.0 6 votes vote down vote up
def serve_data(ds, addr):
    ctx = zmq.Context()
    socket = ctx.socket(zmq.PUSH)
    socket.set_hwm(10)
    socket.bind(addr)
    ds = RepeatedData(ds, -1)
    try:
        ds.reset_state()
        logger.info("Serving data at {}".format(addr))
        while True:
            for dp in ds.get_data():
                socket.send(dumps(dp), copy=False)
    finally:
        socket.setsockopt(zmq.LINGER, 0)
        socket.close()
        if not ctx.closed:
            ctx.destroy(0) 
Example #2
Source File: win32support.py    From Computable with MIT License 6 votes vote down vote up
def forward_read_events(fd, context=None):
    """Forward read events from an FD over a socket.

    This method wraps a file in a socket pair, so it can
    be polled for read events by select (specifically zmq.eventloop.ioloop)
    """
    if context is None:
        context = zmq.Context.instance()
    push = context.socket(zmq.PUSH)
    push.setsockopt(zmq.LINGER, -1)
    pull = context.socket(zmq.PULL)
    addr='inproc://%s'%uuid.uuid4()
    push.bind(addr)
    pull.connect(addr)
    forwarder = ForwarderThread(push, fd)
    forwarder.start()
    return pull 
Example #3
Source File: remote.py    From DDRL with Apache License 2.0 6 votes vote down vote up
def serve_data(ds, addr):
    ctx = zmq.Context()
    socket = ctx.socket(zmq.PUSH)
    socket.set_hwm(10)
    socket.bind(addr)
    ds = RepeatedData(ds, -1)
    try:
        ds.reset_state()
        logger.info("Serving data at {}".format(addr))
        while True:
            for dp in ds.get_data():
                socket.send(dumps(dp), copy=False)
    finally:
        socket.setsockopt(zmq.LINGER, 0)
        socket.close()
        if not ctx.closed:
            ctx.destroy(0) 
Example #4
Source File: remote.py    From VDAIC2017 with MIT License 6 votes vote down vote up
def serve_data(ds, addr):
    ctx = zmq.Context()
    socket = ctx.socket(zmq.PUSH)
    socket.set_hwm(10)
    socket.bind(addr)
    ds = RepeatedData(ds, -1)
    try:
        ds.reset_state()
        logger.info("Serving data at {}".format(addr))
        while True:
            for dp in ds.get_data():
                socket.send(dumps(dp), copy=False)
    finally:
        socket.setsockopt(zmq.LINGER, 0)
        socket.close()
        if not ctx.closed:
            ctx.destroy(0) 
Example #5
Source File: scoopzmq.py    From scoop with GNU Lesser General Public License v3.0 6 votes vote down vote up
def createZMQSocket(self, sock_type):
        """Create a socket of the given sock_type and deactivate message dropping"""
        sock = self.ZMQcontext.socket(sock_type)
        sock.setsockopt(zmq.LINGER, LINGER_TIME)
        sock.setsockopt(zmq.IPV4ONLY, 0)

        # Remove message dropping
        sock.setsockopt(zmq.SNDHWM, 0)
        sock.setsockopt(zmq.RCVHWM, 0)
        try:
            sock.setsockopt(zmq.IMMEDIATE, 1)
        except:
            # This parameter was recently added by new libzmq versions
            pass

        # Don't accept unroutable messages
        if sock_type == zmq.ROUTER:
            sock.setsockopt(zmq.ROUTER_MANDATORY, 1)
        return sock 
Example #6
Source File: jrpc_py.py    From TradeSim with Apache License 2.0 6 votes vote down vote up
def _do_connect(self):

        client_id = str(random.randint(1000000, 100000000))

        socket = self._ctx.socket(zmq.DEALER)
        identity = (client_id) + '$' + str(random.randint(1000000, 1000000000))
        identity = identity.encode('utf-8')
        socket.setsockopt(zmq.IDENTITY, identity)
        socket.setsockopt(zmq.RCVTIMEO, 500)
        socket.setsockopt(zmq.SNDTIMEO, 500)
        socket.setsockopt(zmq.LINGER, 0)
        socket.connect(self._addr)

        return socket 
Example #7
Source File: socket_interface.py    From powerapi with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def _create_socket(self, socket_type, linger_value):
        """
        Create a socket of the given type, bind it to a random port and
        register it to the poller

        :param int socket_type: type of the socket to open
        :param int linger_value: -1 mean wait for receive all msg and block
                                 closing 0 mean hardkill the socket even if msg
                                 are still here.
        :return (zmq.Socket, int): the initialized socket and the port where the
                                   socket is bound
        """
        socket = SafeContext.get_context().socket(socket_type)
        socket.setsockopt(zmq.LINGER, linger_value)
        socket.set_hwm(0)
        port_number = socket.bind_to_random_port(LOCAL_ADDR)
        self.poller.register(socket, zmq.POLLIN)
        self.logger.debug("bind to " + LOCAL_ADDR + ':' + str(port_number))
        return (socket, port_number) 
Example #8
Source File: socket_interface.py    From powerapi with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def connect_data(self):
        """
        Connect to the pull socket of this actor

        Open a push socket on the process that want to communicate with this
        actor

        this method shouldn't be called if socket interface was not initialized
        with the setup method
        """

        if self.pull_socket_address is None:
            self._values_available.wait()
            self.pull_socket_address = LOCAL_ADDR + ':' + str(self._pull_port.value)
            self.control_socket_address = LOCAL_ADDR + ':' + str(self._ctrl_port.value)

        self.push_socket = SafeContext.get_context().socket(zmq.PUSH)
        self.push_socket.setsockopt(zmq.LINGER, -1)
        self.push_socket.set_hwm(0)
        self.push_socket.connect(self.pull_socket_address)
        self.logger.debug("connected data to %s" % (self.pull_socket_address)) 
Example #9
Source File: socket_interface.py    From powerapi with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def connect_control(self):
        """
        Connect to the control socket of this actor

        Open a pair socket on the process that want to control this actor
        this method shouldn't be called if socket interface was not initialized
        with the setup method
        """
        if self.pull_socket_address is None:
            self._values_available.wait()
            self.pull_socket_address = LOCAL_ADDR + ':' + str(self._pull_port.value)
            self.control_socket_address = LOCAL_ADDR + ':' + str(self._ctrl_port.value)

        self.control_socket = SafeContext.get_context().socket(zmq.PAIR)
        self.control_socket.setsockopt(zmq.LINGER, 0)
        self.control_socket.set_hwm(0)
        self.control_socket.connect(self.control_socket_address)
        self.logger.debug("connected control to %s" % (self.control_socket_address)) 
Example #10
Source File: remote.py    From Distributed-BA3C with Apache License 2.0 6 votes vote down vote up
def serve_data(ds, addr):
    ctx = zmq.Context()
    socket = ctx.socket(zmq.PUSH)
    socket.set_hwm(10)
    socket.bind(addr)
    ds = RepeatedData(ds, -1)
    try:
        ds.reset_state()
        logger.info("Serving data at {}".format(addr))
        while True:
            for dp in ds.get_data():
                socket.send(dumps(dp), copy=False)
    finally:
        socket.setsockopt(zmq.LINGER, 0)
        socket.close()
        if not ctx.closed:
            ctx.destroy(0) 
Example #11
Source File: worker.py    From platoon with MIT License 6 votes vote down vote up
def init_mb_sock(self, port, data_hwm=10):
        """
        Initialize the mini-batch data socket.

        Parameters
        ----------
        port : int
           The tcp port to reach the mini-batch server on.
        data_hwm : int, optional
           High water mark, see pyzmq docs.

        .. note::
           This must be called before using :meth:`recv_mb`.

        """
        self.asocket = self.context.socket(zmq.PULL)
        self.asocket.setsockopt(zmq.LINGER, 0)
        self.asocket.set_hwm(data_hwm)
        self.asocket.connect("tcp://localhost:{}".format(port))

        self.apoller = zmq.Poller()
        self.apoller.register(self.asocket, zmq.POLLIN) 
Example #12
Source File: worker.py    From platoon with MIT License 6 votes vote down vote up
def _init_control_socket(self, port):
        """
        Initialize control socket.

        Parameters
        ---------
        port : int
          The tcp port where the control master is listening at.

        .. note::
           This must be called before using :meth:`send_req`.

        """
        self.csocket = self.context.socket(zmq.REQ)
        self.csocket.setsockopt(zmq.LINGER, 0)
        self.csocket.connect('tcp://localhost:{}'.format(port))

        self.cpoller = zmq.Poller()
        self.cpoller.register(self.csocket, zmq.POLLIN)

################################################################################
#                            Collectives Interface                             #
################################################################################ 
Example #13
Source File: zmq.py    From timeflux with MIT License 6 votes vote down vote up
def __init__(
        self, topic, address="tcp://127.0.0.1:5559", serializer="pickle", wait=0
    ):
        """Create a publisher"""
        self._topic = topic.encode("utf-8")
        self._serializer = getattr(timeflux.core.message, serializer + "_serialize")
        try:
            context = zmq.Context.instance()
            self._socket = context.socket(zmq.PUB)
            self._socket.setsockopt(zmq.LINGER, 0)
            self._socket.connect(address)
        except zmq.ZMQError as e:
            self.logger.error(e)

        # Quick fix to the slow joiner syndrome
        # TODO: remove when Last Value Caching is implemented
        # Wait for subscribers to connect
        # http://zguide.zeromq.org/page%3aall#Getting-the-Message-Out
        # http://zguide.zeromq.org/page%3aall#Node-Coordination
        # http://zguide.zeromq.org/page%3aall#Last-Value-Caching
        # https://stackoverflow.com/questions/30864145/zmq-no-subscription-message-on-xpub-socket-for-multiple-subscribers-last-value
        time.sleep(wait) 
Example #14
Source File: test_socket.py    From pySINDy with MIT License 5 votes vote down vote up
def test_attr(self):
        """set setting/getting sockopts as attributes"""
        s = self.context.socket(zmq.DEALER)
        self.sockets.append(s)
        linger = 10
        s.linger = linger
        self.assertEqual(linger, s.linger)
        self.assertEqual(linger, s.getsockopt(zmq.LINGER))
        self.assertEqual(s.fd, s.getsockopt(zmq.FD)) 
Example #15
Source File: master_api.py    From zoe with Apache License 2.0 5 votes vote down vote up
def _disconnect(self):
        self.zmq_s.setsockopt(zmq.LINGER, 0)
        self.zmq_s.close()
        self.poll.unregister(self.zmq_s)
        self.zmq_s = None 
Example #16
Source File: zmq_utils.py    From Jacinle with MIT License 5 votes vote down vote up
def graceful_close(sock):
    if sock is None:
        return
    sock.setsockopt(zmq.LINGER, 0)
    sock.close() 
Example #17
Source File: asn_client.py    From bearded-avenger with Mozilla Public License 2.0 5 votes vote down vote up
def __init__(self, endpoint='tcp://localhost:5555'):
        context = zmq.Context()
        logger.debug("Connecting to asn lookup server")
        socket = context.socket(zmq.DEALER)
        socket.set(zmq.LINGER, 200)
        socket.connect(endpoint)
        self.socket = socket


        self.get_fields() 
Example #18
Source File: remote.py    From pyprob with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def __init__(self, server_address):
        self._server_address = server_address
        self._context = zmq.Context.instance()
        self._socket = self._context.socket(zmq.REQ)
        self._socket.setsockopt(zmq.LINGER, 100)
        print('ppx (Python): zmq.REQ socket connecting to server {}'.format(self._server_address))
        self._socket.connect(self._server_address) 
Example #19
Source File: zcom.py    From tensorcom with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def connect(self, url, topic=""):
        """Explicitly connect to a ZMQ socket.

        :param url: ZMQ-URL to connect to  (Default value = "")
        :param topic: topic to subscribe to for SUB sockets (Default value = "")

        """
        if isinstance(url, (list, tuple)):
            for u in url:
                self.connect(u, topic=topic)
            return
        self.addr = urlparse(url)
        scheme, transport = (self.addr.scheme.split("+", 2) + ["tcp"])[:2]
        kind, bind = schemes[scheme]
        logging.info("kind %s bind %s", kind, bind)
        try:
            if self.socket is None:
                self.socket = self.context.socket(kind)
            location = transport + "://" + self.addr.netloc
            if transport == "ipc":
                location += self.addr.path
            self.socket.setsockopt(zmq.LINGER, 0)
            if bind:
                logging.info("binding to %s", location)
                self.socket.bind(location)
            else:
                logging.info("connecting to %s", location)
                self.socket.connect(location)
            if kind == zmq.SUB:
                logging.info("subscribing to '%s'", topic)
                self.socket.setsockopt_string(zmq.SUBSCRIBE, topic)
        except Exception as e:
            print("error: url {} location {} kind {}".format(url, location, kind))
            raise e 
Example #20
Source File: control_gui_override_mavutil.py    From gr-uaslink with GNU General Public License v3.0 5 votes vote down vote up
def zmq_setup(self,zmq_addr):
        #zmq_addr='tcp://127.0.0.1:4003' #port for receiving messages
        self.timeout=100
        self.ctx = zmq.Context()
        self.zmqc = self.ctx.socket(zmq.PUB)
        self.zmqc.setsockopt(zmq.LINGER, 0)
        self.zmqc.bind(zmq_addr)
        #self.zmqc.connect(zmq_addr)
        #zmqc.setsockopt(zmq.SUBSCRIBE, "") 
Example #21
Source File: control_gui_override_control.py    From gr-uaslink with GNU General Public License v3.0 5 votes vote down vote up
def zmq_setup(self,zmq_addr):
        #zmq_addr='tcp://127.0.0.1:4003' #port for receiving messages
        self.timeout=100
        self.ctx = zmq.Context()
        self.zmqc = self.ctx.socket(zmq.PUB)
        self.zmqc.setsockopt(zmq.LINGER, 0)
        self.zmqc.bind(zmq_addr)
        #self.zmqc.connect(zmq_addr)
        #zmqc.setsockopt(zmq.SUBSCRIBE, "") 
Example #22
Source File: tcpclient.py    From pyModeS with GNU General Public License v3.0 5 votes vote down vote up
def connect(self):
        self.socket = zmq.Context().socket(zmq.STREAM)
        self.socket.setsockopt(zmq.LINGER, 0)
        self.socket.setsockopt(zmq.RCVTIMEO, 10000)
        self.socket.connect("tcp://%s:%s" % (self.host, self.port)) 
Example #23
Source File: jrpc_py.py    From TradeApi with Apache License 2.0 5 votes vote down vote up
def _do_connect(self):

        client_id = str(random.randint(1000000, 100000000))

        socket = self._ctx.socket(zmq.DEALER)
        identity = (client_id) + '$' + str(random.randint(1000000, 1000000000))
        identity = identity.encode('utf-8')
        socket.setsockopt(zmq.IDENTITY, identity)
        socket.setsockopt(zmq.RCVTIMEO, 500)
        socket.setsockopt(zmq.SNDTIMEO, 500)
        socket.setsockopt(zmq.LINGER, 0)
        socket.connect(self._addr)

        return socket 
Example #24
Source File: pause_for_kernel.py    From ansible-jupyter-kernel with Apache License 2.0 5 votes vote down vote up
def run(self, tmp=None, task_vars=None):
        # print ('pause_for_kernel')
        if task_vars is None:
            task_vars = dict()
        host = self._task.args.get('host', None)
        port = self._task.args.get('port', None)
        task_num = self._task.args.get('task_num', None)
        # print (task_num)
        result = super(ActionModule, self).run(tmp, task_vars)

        context = zmq.Context()
        socket = context.socket(zmq.REQ)
        socket.setsockopt(zmq.LINGER, 0)
        # print ('connecting...')
        socket.connect("tcp://{0}:{1}".format(host, port))
        # print ('connected')
        # print ('sending...')
        socket.send_string("{0}".format(task_num))
        # print ('sent')
        # print ('waiting...')
        socket.recv()
        # print ('received')
        # print ('closing...')
        socket.close()
        # print ('closed')
        return result 
Example #25
Source File: remote_agent.py    From neural_chat with MIT License 5 votes vote down vote up
def connect(self):
        """Bind or connect to ZMQ socket. Requires package zmq."""
        context = zmq.Context()
        self.socket = context.socket(self.socket_type)
        self.socket.setsockopt(zmq.LINGER, 1)
        host = 'tcp://{}:{}'.format(self.address, self.port)
        if self.socket_type == zmq.REP:
            self.socket.bind(host)
        else:
            self.socket.connect(host)
        print('python thread connected to ' + host) 
Example #26
Source File: test_log.py    From QuLab with MIT License 5 votes vote down vote up
def test_zmq_handler():
    ctx = zmq.Context()
    interface = "tcp://127.0.0.1"
    with ctx.socket(zmq.PUB) as pub, ctx.socket(zmq.SUB) as sub:
        sub.setsockopt(zmq.LINGER, 0)
        pub.setsockopt(zmq.LINGER, 0)

        port = pub.bind_to_random_port(interface)
        sub.connect('%s:%s' % (interface, port))

        sub.subscribe(b'')
        time.sleep(0.1)

        logger = logging.getLogger('qulabtest')
        logger.setLevel(logging.DEBUG)
        handler = ZMQHandler(pub)
        handler.setLevel(logging.DEBUG)
        logger.addHandler(handler)

        logger.debug('hello')
        if sub.poll(100):
            btopic, bmsg = sub.recv_multipart()
            record = logging.makeLogRecord(unpack(bmsg))
            assert record.msg == 'hello'
        else:
            assert False, "ZMQ time out." 
Example #27
Source File: __init__.py    From pySINDy with MIT License 5 votes vote down vote up
def create_bound_pair(self, type1=zmq.PAIR, type2=zmq.PAIR, interface='tcp://127.0.0.1'):
        """Create a bound socket pair using a random port."""
        s1 = self.context.socket(type1)
        s1.setsockopt(zmq.LINGER, 0)
        port = s1.bind_to_random_port(interface)
        s2 = self.context.socket(type2)
        s2.setsockopt(zmq.LINGER, 0)
        s2.connect('%s:%s' % (interface, port))
        self.sockets.extend([s1,s2])
        return s1, s2 
Example #28
Source File: socket.py    From pySINDy with MIT License 5 votes vote down vote up
def close(self, linger=None):
        rc = 0
        if not self._closed and hasattr(self, '_zmq_socket'):
            if self._zmq_socket is not None:
                if linger is not None:
                    self.set(zmq.LINGER, linger)
                rc = C.zmq_close(self._zmq_socket)
            self._closed = True
            if self.context:
                self.context._rm_socket(self._ref)
        if rc < 0:
            _check_rc(rc) 
Example #29
Source File: test_monqueue.py    From pySINDy with MIT License 5 votes vote down vote up
def test_default_mq_args(self):
        self.device = dev = devices.ThreadMonitoredQueue(zmq.ROUTER, zmq.DEALER, zmq.PUB)
        dev.setsockopt_in(zmq.LINGER, 0)
        dev.setsockopt_out(zmq.LINGER, 0)
        dev.setsockopt_mon(zmq.LINGER, 0)
        # this will raise if default args are wrong
        dev.start()
        self.teardown_device() 
Example #30
Source File: test_context.py    From pySINDy with MIT License 5 votes vote down vote up
def test_sockopts(self):
        """setting socket options with ctx attributes"""
        ctx = self.Context()
        ctx.linger = 5
        self.assertEqual(ctx.linger, 5)
        s = ctx.socket(zmq.REQ)
        self.assertEqual(s.linger, 5)
        self.assertEqual(s.getsockopt(zmq.LINGER), 5)
        s.close()
        # check that subscribe doesn't get set on sockets that don't subscribe:
        ctx.subscribe = b''
        s = ctx.socket(zmq.REQ)
        s.close()
        
        ctx.term()