Python zmq.POLLIN Examples

The following are 30 code examples of zmq.POLLIN(). 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 Project: funcX   Author: funcx-faas   File: zmq_worker.py    License: Apache License 2.0 6 votes vote down vote up
def reconnect_to_broker(self):
        """Connect or reconnect to broker"""
        if self.worker:
            self.poller.unregister(self.worker)
            self.worker.close()
        self.worker = self.ctx.socket(zmq.DEALER)
        self.worker.linger = 0
        self.worker.connect(self.broker)
        self.poller.register(self.worker, zmq.POLLIN)
        if self.verbose:
            logging.info("I: connecting to broker at %s…", self.broker)

        # Register service with broker
        self.send_to_broker(MDP.W_READY, pickle.dumps(self.service), [])

        # If liveness hits zero, queue is considered disconnected
        self.liveness = self.HEARTBEAT_LIVENESS
        self.heartbeat_at = time.time() + 1e-3 * self.heartbeat 
Example #2
Source Project: vnpy_crypto   Author: birforce   File: thread.py    License: MIT License 6 votes vote down vote up
def run(self):
        """Start the Authentication Agent thread task"""
        self.authenticator.start()
        self.started.set()
        zap = self.authenticator.zap_socket
        poller = zmq.Poller()
        poller.register(self.pipe, zmq.POLLIN)
        poller.register(zap, zmq.POLLIN)
        while True:
            try:
                socks = dict(poller.poll())
            except zmq.ZMQError:
                break  # interrupted

            if self.pipe in socks and socks[self.pipe] == zmq.POLLIN:
                terminate = self._handle_pipe()
                if terminate:
                    break

            if zap in socks and socks[zap] == zmq.POLLIN:
                self._handle_zap()

        self.pipe.close()
        self.authenticator.stop() 
Example #3
Source Project: vnpy_crypto   Author: birforce   File: zmqstream.py    License: MIT License 6 votes vote down vote up
def _handle_events(self, fd, events):
        """This method is the actual handler for IOLoop, that gets called whenever
        an event on my socket is posted. It dispatches to _handle_recv, etc."""
        if not self.socket:
            gen_log.warning("Got events for closed stream %s", fd)
            return
        zmq_events = self.socket.EVENTS
        try:
            # dispatch events:
            if zmq_events & zmq.POLLIN and self.receiving():
                self._handle_recv()
                if not self.socket:
                    return
            if zmq_events & zmq.POLLOUT and self.sending():
                self._handle_send()
                if not self.socket:
                    return

            # rebuild the poll state
            self._rebuild_io_state()
        except Exception:
            gen_log.error("Uncaught exception in zmqstream callback",
                          exc_info=True)
            raise 
Example #4
Source Project: vnpy_crypto   Author: birforce   File: _test_asyncio.py    License: MIT License 6 votes vote down vote up
def test_poll(self):
        @asyncio.coroutine
        def test():
            a, b = self.create_bound_pair(zmq.PUSH, zmq.PULL)
            f = b.poll(timeout=0)
            yield from asyncio.sleep(0)
            self.assertEqual(f.result(), 0)

            f = b.poll(timeout=1)
            assert not f.done()
            evt = yield from f

            self.assertEqual(evt, 0)

            f = b.poll(timeout=1000)
            assert not f.done()
            yield from a.send_multipart([b'hi', b'there'])
            evt = yield from f
            self.assertEqual(evt, zmq.POLLIN)
            recvd = yield from b.recv_multipart()
            self.assertEqual(recvd, [b'hi', b'there'])
        self.loop.run_until_complete(test()) 
Example #5
Source Project: vnpy_crypto   Author: birforce   File: _test_asyncio.py    License: MIT License 6 votes vote down vote up
def test_poll_base_socket(self):
        @asyncio.coroutine
        def test():
            ctx = zmq.Context()
            url = 'inproc://test'
            a = ctx.socket(zmq.PUSH)
            b = ctx.socket(zmq.PULL)
            self.sockets.extend([a, b])
            a.bind(url)
            b.connect(url)

            poller = zaio.Poller()
            poller.register(b, zmq.POLLIN)

            f = poller.poll(timeout=1000)
            assert not f.done()
            a.send_multipart([b'hi', b'there'])
            evt = yield from f
            self.assertEqual(evt, [(b, zmq.POLLIN)])
            recvd = b.recv_multipart()
            self.assertEqual(recvd, [b'hi', b'there'])
        self.loop.run_until_complete(test()) 
Example #6
Source Project: vnpy_crypto   Author: birforce   File: test_poll.py    License: MIT License 6 votes vote down vote up
def test_timeout(self):
        """make sure Poller.poll timeout has the right units (milliseconds)."""
        s1, s2 = self.create_bound_pair(zmq.PAIR, zmq.PAIR)
        poller = self.Poller()
        poller.register(s1, zmq.POLLIN)
        tic = time.time()
        evt = poller.poll(.005)
        toc = time.time()
        self.assertTrue(toc-tic < 0.1)
        tic = time.time()
        evt = poller.poll(5)
        toc = time.time()
        self.assertTrue(toc-tic < 0.1)
        self.assertTrue(toc-tic > .001)
        tic = time.time()
        evt = poller.poll(500)
        toc = time.time()
        self.assertTrue(toc-tic < 1)
        self.assertTrue(toc-tic > 0.1) 
Example #7
Source Project: vnpy_crypto   Author: birforce   File: test_future.py    License: MIT License 6 votes vote down vote up
def test_poll_base_socket(self):
        @gen.coroutine
        def test():
            ctx = zmq.Context()
            url = 'inproc://test'
            a = ctx.socket(zmq.PUSH)
            b = ctx.socket(zmq.PULL)
            self.sockets.extend([a, b])
            a.bind(url)
            b.connect(url)

            poller = future.Poller()
            poller.register(b, zmq.POLLIN)

            f = poller.poll(timeout=1000)
            assert not f.done()
            a.send_multipart([b'hi', b'there'])
            evt = yield f
            self.assertEqual(evt, [(b, zmq.POLLIN)])
            recvd = b.recv_multipart()
            self.assertEqual(recvd, [b'hi', b'there'])
            a.close()
            b.close()
            ctx.term()
        self.loop.run_sync(test) 
Example #8
Source Project: networkzero   Author: tjguk   File: test_messenger.py    License: MIT License 6 votes vote down vote up
def support_test_send_to_multiple_addresses(self, address1, address2):
        poller = zmq.Poller()

        socket1 = self.context.socket(roles['listener'])
        socket2 = self.context.socket(roles['listener'])
        try:
            socket1.bind("tcp://%s" % address1)
            socket2.bind("tcp://%s" % address2)
            poller.register(socket1, zmq.POLLIN)
            poller.register(socket2, zmq.POLLIN)
            polled = dict(poller.poll(2000))
            if socket1 in polled:
                socket1.recv()
                socket1.send(nw0.sockets._serialise(address1))
            elif socket2 in polled:
                socket2.recv()
                socket2.send(nw0.sockets._serialise(address2))
            else:
                raise RuntimeError("Nothing found")
        finally:
            socket1.close()
            socket2.close() 
Example #9
Source Project: Computable   Author: ktraunmueller   File: ipkernel.py    License: MIT License 6 votes vote down vote up
def _abort_queue(self, stream):
        poller = zmq.Poller()
        poller.register(stream.socket, zmq.POLLIN)
        while True:
            idents,msg = self.session.recv(stream, zmq.NOBLOCK, content=True)
            if msg is None:
                return

            self.log.info("Aborting:")
            self.log.info("%s", msg)
            msg_type = msg['header']['msg_type']
            reply_type = msg_type.split('_')[0] + '_reply'

            status = {'status' : 'aborted'}
            md = {'engine' : self.ident}
            md.update(status)
            reply_msg = self.session.send(stream, reply_type, metadata=md,
                        content=status, parent=msg, ident=idents)
            self.log.debug("%s", reply_msg)
            # We need to wait a bit for requests to come in. This can probably
            # be set shorter for true asynchronous clients.
            poller.poll(50) 
Example #10
Source Project: Computable   Author: ktraunmueller   File: eventloops.py    License: MIT License 6 votes vote down vote up
def _notify_stream_qt(kernel, stream):

    from IPython.external.qt_for_kernel import QtCore

    if _on_os_x_10_9() and kernel._darwin_app_nap:
        from IPython.external.appnope import nope_scope as context
    else:
        from IPython.core.interactiveshell import NoOpContext as context

    def process_stream_events():
        while stream.getsockopt(zmq.EVENTS) & zmq.POLLIN:
            with context():
                kernel.do_one_iteration()

    fd = stream.getsockopt(zmq.FD)
    notifier = QtCore.QSocketNotifier(fd, QtCore.QSocketNotifier.Read, kernel.app)
    notifier.activated.connect(process_stream_events) 
Example #11
Source Project: cryptotrader   Author: naripok   File: datafeed.py    License: MIT License 6 votes vote down vote up
def __init__(self, exchange='', addr='ipc:///tmp/feed.ipc', timeout=30):
        """

        :param period: int: Data sampling period
        :param pairs: list: Pair symbols to trade
        :param exchange: str: FeedDaemon exchange to query
        :param addr: str: Client socked address
        :param timeout: int:
        """
        super(DataFeed, self).__init__()

        # Sock objects
        self.context = zmq.Context()
        self.addr = addr
        self.exchange = exchange
        self.timeout = timeout * 1000

        self.sock = self.context.socket(zmq.REQ)
        self.sock.connect(addr)

        self.poll = zmq.Poller()
        self.poll.register(self.sock, zmq.POLLIN) 
Example #12
Source Project: BAG_framework   Author: ucb-art   File: zmqwrapper.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def __init__(self, port, pipeline=100, host='localhost', log_file=None):
        """Create a new ZMQDealer object.
        """
        context = zmq.Context.instance()
        # noinspection PyUnresolvedReferences
        self.socket = context.socket(zmq.DEALER)
        self.socket.hwm = pipeline
        self.socket.connect('tcp://%s:%d' % (host, port))
        self._log_file = log_file
        self.poller = zmq.Poller()
        # noinspection PyUnresolvedReferences
        self.poller.register(self.socket, zmq.POLLIN)

        if self._log_file is not None:
            self._log_file = Path(self._log_file).resolve()
            # If log file directory does not exists, create it
            log_dir: Path = self._log_file.parent
            log_dir.mkdir(parents=True, exist_ok=True)
            # time stamp the file
            now = datetime.now()
            time_stamp = now.strftime('%Y%m%d_%H%M%S%f')
            ext = self._log_file.suffix
            self._log_file = str(log_dir / f'{self._log_file.stem}_{time_stamp}{ext}') 
Example #13
Source Project: pymeasure   Author: ralph-group   File: listeners.py    License: MIT License 6 votes vote down vote up
def __init__(self, port, topic='', timeout=0.01):
        """ Constructs the Listener object with a subscriber port
        over which to listen for messages

        :param port: TCP port to listen on
        :param topic: Topic to listen on
        :param timeout: Timeout in seconds to recheck stop flag
        """
        super().__init__()

        self.port = port
        self.topic = topic
        self.context = zmq.Context()
        log.debug("%s has ZMQ Context: %r" % (self.__class__.__name__, self.context))
        self.subscriber = self.context.socket(zmq.SUB)
        self.subscriber.connect('tcp://localhost:%d' % port)
        self.subscriber.setsockopt(zmq.SUBSCRIBE, topic.encode())
        log.info("%s connected to '%s' topic on tcp://localhost:%d" % (
            self.__class__.__name__, topic, port))

        self.poller = zmq.Poller()
        self.poller.register(self.subscriber, zmq.POLLIN)
        self.timeout = timeout 
Example #14
Source Project: pymeasure   Author: ralph-group   File: listeners.py    License: MIT License 6 votes vote down vote up
def __init__(self, port, topic='', timeout=0.01):
        """ Constructs the Listener object with a subscriber port
        over which to listen for messages

        :param port: TCP port to listen on
        :param topic: Topic to listen on
        :param timeout: Timeout in seconds to recheck stop flag
        """
        super().__init__()

        self.port = port
        self.topic = topic
        self.context = zmq.Context()
        log.debug("%s has ZMQ Context: %r" % (self.__class__.__name__, self.context))
        self.subscriber = self.context.socket(zmq.SUB)
        self.subscriber.connect('tcp://localhost:%d' % port)
        self.subscriber.setsockopt(zmq.SUBSCRIBE, topic.encode())
        log.info("%s connected to '%s' topic on tcp://localhost:%d" % (
            self.__class__.__name__, topic, port))

        self.poller = zmq.Poller()
        self.poller.register(self.subscriber, zmq.POLLIN)
        self.timeout = timeout 
Example #15
Source Project: supvisors   Author: julien6387   File: mainloop.py    License: Apache License 2.0 6 votes vote down vote up
def run(self):
        """ Contents of the infinite loop. """
        # Create zmq sockets
        sockets = SupvisorsZmq(self.supvisors)
        # create poller
        poller = zmq.Poller()
        # register sockets
        poller.register(sockets.internal_subscriber.socket, zmq.POLLIN)
        poller.register(sockets.puller.socket, zmq.POLLIN)
        # poll events forever
        while not self.stopping():
            socks = dict(poller.poll(500))
            # test stop condition again: if Supervisor is stopping,
            # any XML-RPC call would block this thread, and the other
            # because of the join
            if not self.stopping():
                self.check_requests(sockets, socks)
                self.check_events(sockets.internal_subscriber, socks)
        # close resources gracefully
        poller.unregister(sockets.puller.socket)
        poller.unregister(sockets.internal_subscriber.socket)
        sockets.close() 
Example #16
Source Project: supvisors   Author: julien6387   File: mainloop.py    License: Apache License 2.0 6 votes vote down vote up
def check_events(self, subscriber, socks):
        """ Forward external Supervisor events to main thread. """
        if subscriber.socket in socks and \
            socks[subscriber.socket] == zmq.POLLIN:
            try:
                message = subscriber.receive()
            except:
                print >> stderr, '[ERROR] failed to get data from subscriber'
            else:
                # The events received are not processed directly in this thread
                # because it would conflict with the processing in the
                # Supervisor thread, as they use the same data.
                # That's why a RemoteCommunicationEvent is used to push the
                # event in the Supervisor thread.
                self.send_remote_comm_event(
                    RemoteCommEvents.SUPVISORS_EVENT,
                    json.dumps(message)) 
Example #17
Source Project: GaragePi   Author: nathanpjones   File: client_api.py    License: MIT License 6 votes vote down vote up
def __send_recv_msg(self, msg):
        # Make sure we're sending bytes instead of strings
        msg = list(map(lambda s: str.encode(s) if type(s) is str else s, msg))
        self.__socket.send_multipart(msg)

        events = dict(self.__poller.poll(SEND_TIMEOUT))
        if events.get(self.__socket) == zmq.POLLIN:
            try:
                # Receive response and make sure the return is converted to string if necessary
                ret_msg = self.__socket.recv_multipart()[0]
                return bytes.decode(ret_msg) if type(ret_msg) is bytes else ret_msg
            except zmq.error.Again:
                # If the receive timed out then return None
                self.__logger.warning("Receive operation timed out!")
                self.__create_socket()
                return None
        else:
            self.__logger.warning("Send operation timed out!")
            self.__create_socket()
            return None 
Example #18
Source Project: stytra   Author: portugueslab   File: __init__.py    License: GNU General Public License v3.0 6 votes vote down vote up
def check_trigger(self):
        """ Wait to receive the json file and reply with the duration of the
        experiment. Then, to the `queue_trigger_params` the received dict,
        so that the `Experiment` can store it with the rest of the data.
        """

        poller = zmq.Poller()
        poller.register(self.zmq_socket, zmq.POLLIN)
        try:
            self.protocol_duration = self.duration_queue.get(timeout=0.0001)
            print(self.protocol_duration)
        except Empty:
            pass
        if poller.poll(10):
            self.scope_config = self.zmq_socket.recv_json()
            self.device_params_queue.put(self.scope_config)
            self.zmq_socket.send_json(self.protocol_duration)
            return True
        else:
            return False 
Example #19
Source Project: pySINDy   Author: luckystarufo   File: thread.py    License: MIT License 6 votes vote down vote up
def run(self):
        """Start the Authentication Agent thread task"""
        self.authenticator.start()
        self.started.set()
        zap = self.authenticator.zap_socket
        poller = zmq.Poller()
        poller.register(self.pipe, zmq.POLLIN)
        poller.register(zap, zmq.POLLIN)
        while True:
            try:
                socks = dict(poller.poll())
            except zmq.ZMQError:
                break  # interrupted

            if self.pipe in socks and socks[self.pipe] == zmq.POLLIN:
                terminate = self._handle_pipe()
                if terminate:
                    break

            if zap in socks and socks[zap] == zmq.POLLIN:
                self._handle_zap()

        self.pipe.close()
        self.authenticator.stop() 
Example #20
Source Project: pySINDy   Author: luckystarufo   File: zmqstream.py    License: MIT License 6 votes vote down vote up
def _handle_events(self, fd, events):
        """This method is the actual handler for IOLoop, that gets called whenever
        an event on my socket is posted. It dispatches to _handle_recv, etc."""
        if not self.socket:
            gen_log.warning("Got events for closed stream %s", fd)
            return
        zmq_events = self.socket.EVENTS
        try:
            # dispatch events:
            if zmq_events & zmq.POLLIN and self.receiving():
                self._handle_recv()
                if not self.socket:
                    return
            if zmq_events & zmq.POLLOUT and self.sending():
                self._handle_send()
                if not self.socket:
                    return

            # rebuild the poll state
            self._rebuild_io_state()
        except Exception:
            gen_log.error("Uncaught exception in zmqstream callback",
                          exc_info=True)
            raise 
Example #21
Source Project: dataflow   Author: tensorpack   File: remote.py    License: Apache License 2.0 5 votes vote down vote up
def __iter__(self):
        with self._guard:
            try:
                ctx = zmq.Context()
                if self._addr2 is None:
                    socket = ctx.socket(zmq.PULL)
                    socket.set_hwm(self._hwm)
                    self.bind_or_connect(socket, self._addr1)

                    while True:
                        dp = loads(socket.recv(copy=False))
                        yield dp
                        self.cnt1 += 1
                else:
                    socket1 = ctx.socket(zmq.PULL)
                    socket1.set_hwm(self._hwm)
                    self.bind_or_connect(socket1, self._addr1)

                    socket2 = ctx.socket(zmq.PULL)
                    socket2.set_hwm(self._hwm)
                    self.bind_or_connect(socket2, self._addr2)

                    poller = zmq.Poller()
                    poller.register(socket1, zmq.POLLIN)
                    poller.register(socket2, zmq.POLLIN)

                    while True:
                        evts = poller.poll()
                        for sock, evt in evts:
                            dp = loads(sock.recv(copy=False))
                            yield dp
                            if sock == socket1:
                                self.cnt1 += 1
                            else:
                                self.cnt2 += 1
            finally:
                ctx.destroy(linger=0)


# for internal use only 
Example #22
Source Project: idasec   Author: RobinDavid   File: broker.py    License: GNU Lesser General Public License v2.1 5 votes vote down vote up
def run_broker_loop_generator(self):
        poll = zmq.Poller()
        poll.register(self.binsec_socket, zmq.POLLIN)
        poll.register(self.pinsec_socket, zmq.POLLIN)
        # print "start iterating"
        try:
            while True:
                sockets = dict(poll.poll(timeout=100))
                if sockets == {}:
                    yield None, None, None
                if self.stop:
                    print "Thread loop stop"
                    break

                if self.pinsec_socket in sockets:
                    cmd, data = self.pinsec_socket.recv_multipart()
                    yield PINSEC, cmd, data
                    # self.pinsec_callback(cmd, data)

                if self.binsec_socket in sockets:
                    cmd, data = self.binsec_socket.recv_multipart()
                    yield BINSEC, cmd, data
                    # self.binsec_callback(cmd, msg)

        except KeyboardInterrupt:
            self.binsec_socket.close()
            self.pinsec_socket.close() 
Example #23
Source Project: funcX   Author: funcx-faas   File: funcx_worker.py    License: Apache License 2.0 5 votes vote down vote up
def __init__(self, worker_id, address, port, logdir, debug=False, worker_type='RAW'):

        self.worker_id = worker_id
        self.address = address
        self.port = port
        self.logdir = logdir
        self.debug = debug
        self.worker_type = worker_type
        self.serializer = FuncXSerializer()
        self.serialize = self.serializer.serialize
        self.deserialize = self.serializer.deserialize

        global logger
        logger = set_file_logger('{}/funcx_worker_{}.log'.format(logdir, worker_id),
                                 name="worker_log",
                                 level=logging.DEBUG if debug else logging.INFO)

        logger.info('Initializing worker {}'.format(worker_id))
        logger.info('Worker is of type: {}'.format(worker_type))

        if debug:
            logger.debug('Debug logging enabled')

        self.context = zmq.Context()
        self.poller = zmq.Poller()
        self.identity = worker_id.encode()

        self.task_socket = self.context.socket(zmq.DEALER)
        self.task_socket.setsockopt(zmq.IDENTITY, self.identity)

        logger.info('Trying to connect to : tcp://{}:{}'.format(self.address, self.port))
        self.task_socket.connect('tcp://{}:{}'.format(self.address, self.port))
        self.poller.register(self.task_socket, zmq.POLLIN) 
Example #24
Source Project: westpa   Author: westpa   File: core.py    License: MIT License 5 votes vote down vote up
def recv_message(self, socket, flags=0, validate=True, timeout=None):
        '''Receive a message object from the given socket, using the given flags.
        Message validation is performed if ``validate`` is true.
        If ``timeout`` is given, then it is the number of milliseconds to wait
        prior to raising a ZMQWMTimeout exception. ``timeout`` is ignored if
        ``flags`` includes ``zmq.NOBLOCK``.'''
        
        if timeout is None or flags & zmq.NOBLOCK:
            message = socket.recv_pyobj(flags)
        else:        
            poller = zmq.Poller()
            poller.register(socket, zmq.POLLIN)
            try:
                poll_results = dict(poller.poll(timeout=timeout))
                if socket in poll_results:
                    message = socket.recv_pyobj(flags)
                else:
                    raise ZMQWMTimeout('recv timed out')
            finally:
                poller.unregister(socket)
        
        if self._super_debug:
            self.log.debug('received {!r}'.format(message))
        if validate:
            with self.message_validation(message):
                self.validate_message(message)
        return message 
Example #25
Source Project: heralding   Author: johnnykv   File: base_logger.py    License: GNU General Public License v3.0 5 votes vote down vote up
def start(self):
    context = heralding.misc.zmq_context

    internal_reporting_socket = context.socket(zmq.SUB)
    internal_reporting_socket.connect(SocketNames.INTERNAL_REPORTING.value)
    internal_reporting_socket.setsockopt(zmq.SUBSCRIBE, b'')

    poller = zmq.Poller()
    poller.register(internal_reporting_socket, zmq.POLLIN)
    while self.enabled:
      socks = dict(poller.poll(500))
      self._execute_regulary()
      if internal_reporting_socket in socks and socks[
          internal_reporting_socket] == zmq.POLLIN:
        data = internal_reporting_socket.recv_pyobj()
        # if None is received, this means that ReportingRelay is going down
        if not data:
          self.stop()
        elif data['message_type'] == 'auth':
          self.handle_auth_log(data['content'])
        elif data['message_type'] == 'session_info':
          self.handle_session_log(data['content'])
        elif data['message_type'] == 'listen_ports':
          self.handle_listen_ports(data['content'])
        elif data['message_type'] == 'aux_info':
          self.handle_auxiliary_log(data['content'])
    internal_reporting_socket.close()
    # at this point we know no more data will arrive.
    self.loggerStopped() 
Example #26
Source Project: vnpy_crypto   Author: birforce   File: __init__.py    License: MIT License 5 votes vote down vote up
def start(self):
        """Start ZAP authentication"""
        super().start()
        self.__poller = Poller()
        self.__poller.register(self.zap_socket, zmq.POLLIN)
        self.__task = asyncio.ensure_future(self.__handle_zap()) 
Example #27
Source Project: vnpy_crypto   Author: birforce   File: zmqstream.py    License: MIT License 5 votes vote down vote up
def on_recv(self, callback, copy=True):
        """Register a callback for when a message is ready to recv.
        
        There can be only one callback registered at a time, so each
        call to `on_recv` replaces previously registered callbacks.
        
        on_recv(None) disables recv event polling.
        
        Use on_recv_stream(callback) instead, to register a callback that will receive
        both this ZMQStream and the message, instead of just the message.
        
        Parameters
        ----------
        
        callback : callable
            callback must take exactly one argument, which will be a
            list, as returned by socket.recv_multipart()
            if callback is None, recv callbacks are disabled.
        copy : bool
            copy is passed directly to recv, so if copy is False,
            callback will receive Message objects. If copy is True,
            then callback will receive bytes/str objects.
        
        Returns : None
        """
        
        self._check_closed()
        assert callback is None or callable(callback)
        self._recv_callback = stack_context.wrap(callback)
        self._recv_copy = copy
        if callback is None:
            self._drop_io_state(zmq.POLLIN)
        else:
            self._add_io_state(zmq.POLLIN) 
Example #28
Source Project: vnpy_crypto   Author: birforce   File: zmqstream.py    License: MIT License 5 votes vote down vote up
def _rebuild_io_state(self):
        """rebuild io state based on self.sending() and receiving()"""
        if self.socket is None:
            return
        state = 0
        if self.receiving():
            state |= zmq.POLLIN
        if self.sending():
            state |= zmq.POLLOUT

        self._state = state
        self._update_handler(state) 
Example #29
Source Project: vnpy_crypto   Author: birforce   File: _future.py    License: MIT License 5 votes vote down vote up
def _add_recv_event(self, kind, kwargs=None, future=None):
        """Add a recv event, returning the corresponding Future"""
        f = future or self._Future()
        if kind.startswith('recv') and kwargs.get('flags', 0) & _zmq.DONTWAIT:
            # short-circuit non-blocking calls
            recv = getattr(self._shadow_sock, kind)
            try:
                r = recv(**kwargs)
            except Exception as e:
                f.set_exception(e)
            else:
                f.set_result(r)
            return f

        # we add it to the list of futures before we add the timeout as the
        # timeout will remove the future from recv_futures to avoid leaks
        self._recv_futures.append(
            _FutureEvent(f, kind, kwargs, msg=None)
        )

        # Don't let the Future sit in _recv_events after it's done
        f.add_done_callback(lambda f: self._remove_finished_future(f, self._recv_futures))

        if hasattr(_zmq, 'RCVTIMEO'):
            timeout_ms = self._shadow_sock.rcvtimeo
            if timeout_ms >= 0:
                self._add_timeout(f, timeout_ms * 1e-3)

        if self._shadow_sock.EVENTS & POLLIN:
            # recv immediately, if we can
            self._handle_recv()
        if self._recv_futures:
            self._add_io_state(POLLIN)
        return f 
Example #30
Source Project: vnpy_crypto   Author: birforce   File: _future.py    License: MIT License 5 votes vote down vote up
def _handle_recv(self):
        """Handle recv events"""
        if not self._shadow_sock.EVENTS & POLLIN:
            # event triggered, but state may have been changed between trigger and callback
            return
        f = None
        while self._recv_futures:
            f, kind, kwargs, _ = self._recv_futures.popleft()
            f._pyzmq_popped = True
            # skip any cancelled futures
            if f.done():
                f = None
            else:
                break
        
        if not self._recv_futures:
            self._drop_io_state(POLLIN)
        
        if f is None:
            return
        
        if kind == 'poll':
            # on poll event, just signal ready, nothing else.
            f.set_result(None)
            return
        elif kind == 'recv_multipart':
            recv = self._shadow_sock.recv_multipart
        elif kind == 'recv':
            recv = self._shadow_sock.recv
        else:
            raise ValueError("Unhandled recv event type: %r" % kind)
        
        kwargs['flags'] |= _zmq.DONTWAIT
        try:
            result = recv(**kwargs)
        except Exception as e:
            f.set_exception(e)
        else:
            f.set_result(result)