示例#1
0
文件: logging.py 项目: adbmd/omegaml
    def _get_fixed_stdout(self):
        """
        this is to support ipykernel stdout
        """
        import sys
        from ipykernel import iostream

        if isinstance(sys.stdout, iostream.OutStream):
            stdout = iostream.OutStream(sys.stdout.session,
                                        sys.stdout.pub_thread, 'omega-logger')
            parent = dict(sys.stdout.parent_header)
            stdout.set_parent(parent)
        else:
            stdout = sys.stdout
        return stdout
示例#2
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)