How to use the zmq.Context.instance function in zmq

To help you get started, we’ve selected a few zmq examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github 3liz / py-qgis-server / pyqgisserver / zeromq / broker.py View on Github external
def run_broker( inaddr: str, outaddr: str, maxqueue: int=100, timeout: int=3000) -> None:
    """ Create a ROUTER-ROUTER broker

        :param inaddr: frontend address to bind to
        :param outaddr: backend address to bind to

        If the max number of waiting request is reached: extra incoming requests will
    """
    # Convert timeout to seconds
    timeout = timeout/1000.0

    context = zmq.Context.instance()

    LOGGER.info("Binding frontend to %s", inaddr)
    frontend = context.socket(zmq.ROUTER)
    frontend.setsockopt(zmq.LINGER, 500)
    frontend.setsockopt(zmq.ROUTER_MANDATORY,1)
    frontend.bind(inaddr)

    LOGGER.info("Binding backend to %s", outaddr)
    backend = context.socket(zmq.ROUTER)
    backend.setsockopt(zmq.LINGER, 500)
    backend.setsockopt(zmq.ROUTER_MANDATORY,1)
    backend.bind(outaddr)

    poller = zmq.Poller()    

    # Only poll for requests from backend until workers are available
github onitu / onitu / onitu / plug / client / heartbeat.py View on Github external
def __init__(self, identity, callback, hb_addr):
        super(HeartBeat, self).__init__()
        ctx = zmq.Context.instance()
        self.socket = ctx.socket(zmq.REQ)
        #self.socket.connect('tcp://127.0.0.1:20005')
        self.socket.connect(hb_addr)
        self.identity = identity

        self.callback = callback
        self._stop = threading.Event()
github TUDelft-CNS-ATM / bluesky / bluesky / manager / client.py View on Github external
def __init__(self):
        ctx = zmq.Context.instance()
        self.event_io  = ctx.socket(zmq.DEALER)
        self.stream_in = ctx.socket(zmq.SUB)
        self.poller    = zmq.Poller()
        self.host_id   = ''
        self.client_id = 0
        self.sender_id = ''

        # Signals
        self.event_received  = Signal()
        self.stream_received = Signal()
github 3liz / py-qgis-server / pyqgisserver / zeromq / worker.py View on Github external
"""
    ctx = zmq.Context.instance()

    LOGGER.info("Connecting to %s", address)
    sock = ctx.socket(zmq.DEALER)
    sock.setsockopt(zmq.LINGER, 500)    # Needed for socket no to wait on close
    sock.setsockopt(zmq.SNDHWM, 1)      # Max 1 item on send queue
    sock.setsockopt(zmq.IMMEDIATE, 1)   # Do not queue if no peer, will block on send
    sock.setsockopt(zmq.RCVTIMEO, 1000) # Heartbeat
    sock.identity = identity or uuid.uuid1().bytes
    LOGGER.info("Identity set to %s", sock.identity)
    sock.connect(address)
   
    if broadcastaddr:
        LOGGER.info("Enabling broadcast notification")
        ctx = zmq.Context.instance()
        sub = ctx.socket(zmq.SUB)
        sub.setsockopt(zmq.LINGER, 500)    # Needed for socket no to wait on close
        sub.setsockopt(zmq.SUBSCRIBE, b'RESTART')
        sub.connect(broadcastaddr)

    # Initialize supervisor client
    supervisor = SupervisorClient()

    try:
        LOGGER.info("Starting ZMQ worker loop")
        while True:
            try:
                sock.send(WORKER_READY)
                client_id, corr_id, request = sock.recv_multipart()
                LOGGER.debug("RCV %s: %s", client_id, corr_id)
                request = pickle.loads(request)
github curtiswest / pepi / communication / communication.py View on Github external
def __init__(self, socket_type):
        if not isinstance(socket_type, int):
            msg = 'Socket_type must be an int, not {}'.format(type(socket_type))
            logging.warn(msg)
            raise TypeError(msg)
        self._context = zmq.Context.instance()
        self.type = socket_type
        try:
            self._socket = self._context.socket(socket_type=socket_type)
        except zmq.ZMQError as e:
            msg = "Couldn't construct ZMQ socket. Incorrect socket_type value?: {}".format(socket_type)
            logging.warn("Couldn't construct ZMQ socket. Incorrect socket_type value?: {}".format(socket_type))
            raise ValueError(msg, e)
        self.log = logging.getLogger(__name__)
        self._socket.identity = self.generate_id()
        self.data_wrapper_func = None
        self.data_wrapper_args = None
github lkorigin / laniakea / src / lighthouse / lighthouse / events_publisher.py View on Github external
def __init__(self, endpoints, pub_queue):
        from laniakea import LocalConfig, LkModule
        from laniakea.msgstream.signing import NACL_ED25519, decode_signing_key_base64, \
            keyfile_read_signing_key

        lconf = LocalConfig()
        self._sockets = []
        self._endpoints = endpoints
        self._ctx = zmq.Context.instance()
        self._pub_queue = pub_queue

        # load our own signing key, so we can sign outgoing messages
        keyfile = lconf.secret_curve_keyfile_for_module(LkModule.LIGHTHOUSE)

        self._signer_id = None
        self._signing_key = None
        if os.path.isfile(keyfile):
            self._signer_id, self._signing_key = keyfile_read_signing_key(keyfile)

        if not self._signing_key:
            log.warning('Can not sign outgoing messages: No valid signing key found for this module.')
        else:
            if type(self._signing_key) is str:
                self._signing_key = decode_signing_key_base64(NACL_ED25519, self._signing_key)
github ipython / ipyparallel / examples / iopubwatcher.py View on Github external
def main(connection_file):
    """watch iopub channel, and print messages"""

    ctx = zmq.Context.instance()

    with open(connection_file) as f:
        cfg = json.loads(f.read())

    reg_url = cfg['interface']
    iopub_port = cfg['iopub']
    iopub_url = "%s:%s"%(reg_url, iopub_port)

    session = Session(key=cfg['key'].encode('ascii'))
    sub = ctx.socket(zmq.SUB)

    # This will subscribe to all messages:
    sub.SUBSCRIBE = b''
    # replace with b'' with b'engine.1.stdout' to subscribe only to engine 1's stdout
    # 0MQ subscriptions are simple 'foo*' matches, so 'engine.1.' subscribes
    # to everything from engine 1, but there is no way to subscribe to
github zeromq / pyzmq / zmq / web / proxy.py View on Github external
def __init__(self, loop=None, context=None):
        self.loop = loop if loop is not None else IOLoop.instance()
        self.context = context if context is not None else zmq.Context.instance()
        self._callbacks = {}
        self.socket = self.context.socket(zmq.DEALER)
        self.stream = ZMQStream(self.socket, self.loop)
        self.stream.on_recv(self._handle_reply)
        self.urls = []
github ipython / ipyparallel / ipyparallel / apps / ipcontrollerapp.py View on Github external
def forward_logging(self):
        if self.log_url:
            self.log.info("Forwarding logging to %s"%self.log_url)
            context = zmq.Context.instance()
            lsock = context.socket(zmq.PUB)
            lsock.connect(self.log_url)
            handler = PUBHandler(lsock)
            handler.root_topic = 'controller'
            handler.setLevel(self.log_level)
            self.log.addHandler(handler)
github sagemath / sagecell / kernel_dealer.py View on Github external
def __init__(self, dealer, id, connection, lifespan, timeout):
        self._on_stop = None
        self._dealer = dealer
        self.id = id
        self.executing = 0
        self.status = "starting"
        now = time.time()
        self.hard_deadline = now + lifespan
        self.timeout = timeout
        if timeout > 0:
            self.deadline = now + self.timeout
        self.session = jupyter_client.session.Session(key=connection["key"])
        self.channels = {}
        context = zmq.Context.instance()
        address = connection["ip"]
        if ":" in address:
            address = "[{}]".format(address)
        for channel, socket_type in (
                ("shell", zmq.DEALER), ("iopub", zmq.SUB), ("hb", zmq.REQ)):
            socket = context.socket(socket_type)
            socket.connect("tcp://{}:{}".format(address, connection[channel]))
            stream = zmq.eventloop.zmqstream.ZMQStream(socket)
            stream.channel = channel
            self.channels[channel] = stream
        self.channels["iopub"].socket.subscribe(b"")
        self.start_hb()
        logger.debug("KernelConnection initialized")