Python zmq.proxy() Examples

The following are 12 code examples of zmq.proxy(). 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: zmqserver.py    From aiotools with MIT License 6 votes vote down vote up
def router_main(_, pidx, args):
    log = get_logger('examples.zmqserver.extra', pidx)
    zctx = zmq.Context()
    zctx.linger = 0
    in_sock = zctx.socket(zmq.PULL)
    in_sock.bind('tcp://*:5033')
    out_sock = zctx.socket(zmq.PUSH)
    out_sock.bind('ipc://example-events')
    try:
        log.info('router proxy started')
        zmq.proxy(in_sock, out_sock)
    except KeyboardInterrupt:
        pass
    except Exception:
        log.exception('unexpected error')
    finally:
        for _ in range(num_workers):
            out_sock.send(b'')  # sentinel
        log.info('router proxy terminated')
        in_sock.close()
        out_sock.close()
        zctx.term()
        os.unlink('example-events') 
Example #2
Source File: paramserver.py    From hfsoftmax with MIT License 6 votes vote down vote up
def run(self):
        context = zmq.Context()
        frontend = context.socket(zmq.ROUTER)
        frontend.bind('tcp://*:5570')

        backend = context.socket(zmq.DEALER)
        backend.bind('inproc://backend')

        worker = ParameterWorker(context)
        worker.start()

        try:
            zmq.proxy(frontend, backend)
        except zmq.ContextTerminated:
            frontend.close()
            backend.close() 
Example #3
Source File: zmq.py    From timeflux with MIT License 6 votes vote down vote up
def __init__(
        self,
        address_in="tcp://127.0.0.1:5559",
        address_out="tcp://127.0.0.1:5560",
        timeout=5,
    ):

        self._timeout = timeout

        try:

            # Capture
            address_monitor = "inproc://monitor"
            context = zmq.Context.instance()
            self._monitor = context.socket(zmq.PULL)
            self._monitor.bind(address_monitor)

            # Proxy
            proxy = ThreadProxy(zmq.XSUB, zmq.XPUB, zmq.PUSH)
            proxy.bind_in(address_in)
            proxy.bind_out(address_out)
            proxy.connect_mon(address_monitor)
            # proxy.setsockopt_mon(zmq.CONFLATE, True) # Do not clutter the network
            proxy.start()

        except zmq.ZMQError as error:
            self.logger.error(error) 
Example #4
Source File: zmq.py    From timeflux with MIT License 6 votes vote down vote up
def update(self):
        """Monitor proxy"""
        if self._timeout == 0:
            pass
        now = time.time()
        count = 0
        try:
            while True:
                self._monitor.recv_multipart(zmq.NOBLOCK, copy=False)
                self._last_event = now
                count += 1
        except zmq.ZMQError:
            if count > 0:
                self.logger.debug("Received %d messages", count)
            if (now - self._last_event) > self._timeout:
                raise WorkerInterrupt("No data after %d seconds" % self._timeout) 
Example #5
Source File: proxydevice.py    From vnpy_crypto with MIT License 5 votes vote down vote up
def run_device(self):
        ins,outs,mons = self._setup_sockets()
        zmq.proxy(ins, outs, mons) 
Example #6
Source File: proxydevice.py    From Computable with MIT License 5 votes vote down vote up
def run_device(self):
        ins,outs,mons = self._setup_sockets()
        zmq.proxy(ins, outs, mons) 
Example #7
Source File: datafeed.py    From cryptotrader with MIT License 5 votes vote down vote up
def run(self):
        try:
            Logger.info(FeedDaemon, "Starting Feed Daemon...")

            # Socket to talk to clients
            clients = self.context.socket(zmq.ROUTER)
            clients.bind(self.addr)

            # Socket to talk to workers
            workers = self.context.socket(zmq.DEALER)
            workers.bind("inproc://workers.inproc")

            # Launch pool of worker threads
            for i in range(self.n_workers):
                thread = threading.Thread(target=self.worker, args=())
                thread.start()

            Logger.info(FeedDaemon.run, "Feed Daemon running. Serving on %s" % self.addr)

            zmq.proxy(clients, workers)

        except KeyboardInterrupt:
            clients.close()
            workers.close()
            self.context.term()

# Client 
Example #8
Source File: proxydevice.py    From pySINDy with MIT License 5 votes vote down vote up
def run_device(self):
        ins,outs,mons = self._setup_sockets()
        zmq.proxy(ins, outs, mons) 
Example #9
Source File: paramserver.py    From hfsoftmax with MIT License 5 votes vote down vote up
def run(self):
        self._socket = self.context.socket(zmq.DEALER)
        self._socket.connect('inproc://backend')
        print('Worker started')
        while True:
            self._recv()

        self._socket.close()
        # since `proxy` is daemon
        # we have to teminate it in a not graceful way
        if self.context:
            print('Terminate proxy ... ')
            time.sleep(1.)  # make sure proxy pass the last msg to clients
            self.context.term() 
Example #10
Source File: queue.py    From crankycoin with MIT License 5 votes vote down vote up
def start_queue(cls):
        try:
            context = zmq.Context(1)
            # Socket facing producers
            frontend = context.socket(zmq.PULL)
            frontend.bind(cls.QUEUE_BIND_IN)
            # Socket facing consumers
            backend = context.socket(zmq.PUSH)
            backend.bind(cls.QUEUE_BIND_OUT)

            zmq.proxy(frontend, backend)

        except Exception as e:
            logger.error("could not start queue: %s", e)
            raise 
Example #11
Source File: zmq.py    From timeflux with MIT License 5 votes vote down vote up
def update(self):
        """Start a blocking proxy."""
        zmq.proxy(self._frontend, self._backend) 
Example #12
Source File: proxydevice.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def run_device(self):
        ins,outs,mons = self._setup_sockets()
        zmq.proxy(ins, outs, mons)