Example #1
0
def connect_qtconsole(connection_info, name=None, extra_args=None):
    """Open a QtConsole connected to a worker who has the given future

    - identify worker with who_has
    - start IPython kernel on the worker
    - start qtconsole connected to the kernel
    """
    runtime_dir = jupyter_runtime_dir()
    if name is None:
        name = uuid4().hex

    path = os.path.join(runtime_dir, name + ".json")
    write_connection_file(path, **connection_info)
    cmd = ["jupyter", "qtconsole", "--existing", path]
    if extra_args:
        cmd.extend(extra_args)
    Popen(cmd)

    @atexit.register
    def _cleanup_connection_file():
        """Cleanup our connection file when we exit."""
        try:
            os.remove(path)
        except OSError:
            pass
Example #2
0
 def write_connection_file(self):
     """write connection info to JSON file"""
     cf = self.abs_connection_file
     self.log.debug("Writing connection file: %s", cf)
     write_connection_file(cf, ip=self.ip, key=self.session.key, transport=self.transport,
     shell_port=self.shell_port, stdin_port=self.stdin_port, hb_port=self.hb_port,
     iopub_port=self.iopub_port, control_port=self.control_port)
 def write_connection_file(self):
     """write connection info to JSON file"""
     cf = self.abs_connection_file
     self.log.debug("Writing connection file: %s", cf)
     write_connection_file(cf, ip=self.ip, key=self.session.key, transport=self.transport,
     shell_port=self.shell_port, stdin_port=self.stdin_port, hb_port=self.hb_port,
     iopub_port=self.iopub_port, control_port=self.control_port)
Example #4
0
def connect_qtconsole(connection_info, name=None, extra_args=None):
    """Open a QtConsole connected to a worker who has the given future

    - identify worker with who_has
    - start IPython kernel on the worker
    - start qtconsole connected to the kernel
    """
    runtime_dir = jupyter_runtime_dir()
    if name is None:
        name = uuid4().hex

    path = os.path.join(runtime_dir, name + '.json')
    write_connection_file(path, **connection_info)
    cmd = ['jupyter', 'qtconsole', '--existing', path]
    if extra_args:
        cmd.extend(extra_args)
    Popen(cmd)
    def _cleanup_connection_file():
        """Cleanup our connection file when we exit."""
        try:
            os.remove(path)
        except OSError:
            pass
    atexit.register(_cleanup_connection_file)
Example #5
0
    def __init__(self, overlayList, displayCtx, frame):
        """Set up the kernel and ``zmq`` ports. A jupyter connection file
        containing the information needed to connect to the kernel is saved
        to a temporary file - its path is accessed as an attribute
        called :meth:`connfile`.

        :arg overlayList: The :class:`.OverlayList`.
        :arg displayCtx:  The master :class:`.DisplayContext`.
        :arg frame:       The :class:`.FSLeyesFrame`.
        """

        ip = '127.0.0.1'
        transport = 'tcp'
        addr = '{}://{}'.format(transport, ip)
        self.__connfile = None
        self.__kernel = None
        self.__error = None
        self.__lastIter = 0
        self.__overlayList = overlayList
        self.__displayCtx = displayCtx
        self.__frame = frame
        self.__env = runscript.fsleyesScriptEnvironment(
            frame, overlayList, displayCtx)[1]

        self.__env['screenshot'] = self.__screenshot

        with warnings.catch_warnings():
            warnings.simplefilter('ignore', category=DeprecationWarning)

            # Use an empty key to disable message signing
            session = jcsession.Session(key=b'')
            context = zmq.Context.instance()

            # create sockets for kernel communication
            shellsock = context.socket(zmq.ROUTER)
            stdinsock = context.socket(zmq.ROUTER)
            controlsock = context.socket(zmq.ROUTER)
            iopubsock = context.socket(zmq.PUB)

            shellstrm = zmqstream.ZMQStream(shellsock)
            controlstrm = zmqstream.ZMQStream(controlsock)

            # I/O and heartbeat communication
            # are managed by separate threads.
            self.__iopub = iostream.IOPubThread(iopubsock)
            self.__heartbeat = heartbeat.Heartbeat(zmq.Context(),
                                                   (transport, ip, 0))
            iopubsock = self.__iopub.background_socket

            self.__heartbeat.start()
            self.__iopub.start()

            # Streams which redirect stdout/
            # stderr to the iopub socket
            stdout = iostream.OutStream(session, self.__iopub, u'stdout')
            stderr = iostream.OutStream(session, self.__iopub, u'stderr')

            # TCP ports for all sockets
            shellport = shellsock.bind_to_random_port(addr)
            stdinport = stdinsock.bind_to_random_port(addr)
            controlport = controlsock.bind_to_random_port(addr)
            iopubport = iopubsock.bind_to_random_port(addr)
            hbport = self.__heartbeat.port

            # Create the kernel
            self.__kernel = FSLeyesIPythonKernel.instance(
                stdout,
                stderr,
                shell_class=FSLeyesIPythonShell,
                session=session,
                shell_streams=[shellstrm, controlstrm],
                iopub_socket=iopubsock,
                stdin_socket=stdinsock,
                user_ns=self.__env,
                log=logging.getLogger('ipykernel.kernelbase'))

        # write connection file to a temp dir
        hd, fname = tempfile.mkstemp(prefix='fsleyes-kernel-{}.json'.format(
            os.getpid()),
                                     suffix='.json')
        os.close(hd)

        self.__connfile = fname

        log.debug('IPython kernel connection file: %s', fname)

        jc.write_connection_file(fname,
                                 shell_port=shellport,
                                 stdin_port=stdinport,
                                 iopub_port=iopubport,
                                 control_port=controlport,
                                 hb_port=hbport,
                                 ip=ip)

        atexit.register(os.remove, self.__connfile)