当前位置: 首页>>代码示例>>Python>>正文


Python InProcessKernelManager.shutdown_kernel方法代码示例

本文整理汇总了Python中IPython.kernel.inprocess.manager.InProcessKernelManager.shutdown_kernel方法的典型用法代码示例。如果您正苦于以下问题:Python InProcessKernelManager.shutdown_kernel方法的具体用法?Python InProcessKernelManager.shutdown_kernel怎么用?Python InProcessKernelManager.shutdown_kernel使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在IPython.kernel.inprocess.manager.InProcessKernelManager的用法示例。


在下文中一共展示了InProcessKernelManager.shutdown_kernel方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_interface

# 需要导入模块: from IPython.kernel.inprocess.manager import InProcessKernelManager [as 别名]
# 或者: from IPython.kernel.inprocess.manager.InProcessKernelManager import shutdown_kernel [as 别名]
    def test_interface(self):
        """ Does the in-process kernel manager implement the basic KM interface?
        """
        km = InProcessKernelManager()
        self.assert_(not km.has_kernel)

        km.start_kernel()
        self.assert_(km.has_kernel)
        self.assert_(km.kernel is not None)

        kc = BlockingInProcessKernelClient(kernel=km.kernel)
        self.assert_(not kc.channels_running)

        kc.start_channels()
        self.assert_(kc.channels_running)

        old_kernel = km.kernel
        km.restart_kernel()
        self.assertIsNotNone(km.kernel)
        self.assertNotEquals(km.kernel, old_kernel)

        km.shutdown_kernel()
        self.assert_(not km.has_kernel)

        self.assertRaises(NotImplementedError, km.interrupt_kernel)
        self.assertRaises(NotImplementedError, km.signal_kernel, 9)

        kc.stop_channels()
        self.assert_(not kc.channels_running)
开发者ID:JohnGriffiths,项目名称:ipython,代码行数:31,代码来源:test_kernelmanager.py

示例2: NotebookRunner

# 需要导入模块: from IPython.kernel.inprocess.manager import InProcessKernelManager [as 别名]
# 或者: from IPython.kernel.inprocess.manager.InProcessKernelManager import shutdown_kernel [as 别名]
class NotebookRunner(object):
    # The kernel communicates with mime-types while the notebook
    # uses short labels for different cell types. We'll use this to
    # map from kernel types to notebook format types.

    MIME_MAP = {
        'image/jpeg': 'jpeg',
        'image/png': 'png',
        'text/plain': 'text',
        'text/html': 'html',
        'text/latex': 'latex',
        'application/javascript': 'html',
    }

    def __init__(self):
        self.km = InProcessKernelManager()
        self.km.start_kernel()

        if platform.system() == 'Darwin':
            # There is sometimes a race condition where the first
            # execute command hits the kernel before it's ready.
            # It appears to happen only on Darwin (Mac OS) and an
            # easy (but clumsy) way to mitigate it is to sleep
            # for a second.
            sleep(1)

        self.kc = self.km.client()
        self.kc.start_channels()

        self.shell = self.kc.shell_channel
        self.iopub = self.kc.iopub_channel
        self.kc.kernel.shell.enable_matplotlib('inline')

        self.shell.execute(WARMUP)
        print("Result: {!r}".format(self.shell.get_msg()))

        try:
            while True:
                msg = self.iopub.get_msg(timeout=0)
                print("iopub Message: {!r}".format(msg))
        except Empty:
            pass

    def __del__(self):
        self.kc.stop_channels()
        self.km.shutdown_kernel()

    def run_cell(self, cell, autosave):
        '''
        Run a notebook cell and update the output of that cell in-place.
        '''
        logging.info('Running cell:\n%s\n', cell.input)
        self.shell.execute(cell.input)

        cell['outputs'] = []
        while True:
            try:
                msg = self.iopub.get_msg(timeout=1)
                if msg['msg_type'] == 'status':
                    if msg['content']['execution_state'] == 'idle':
                        break
            except Empty:
                pass

            content = msg['content']
            msg_type = msg['msg_type']

            out = NotebookNode(output_type=msg_type)

            if 'execution_count' in content:
                cell['prompt_number'] = content['execution_count'] - 1
                out.prompt_number = content['execution_count'] - 1

            if msg_type in ['status', 'pyin']:
                continue
            elif msg_type == 'stream':
                out.stream = content['name']
                out.text = content['data']
                #print(out.text, end='')
            elif msg_type in ('display_data', 'pyout'):
                for mime, data in content['data'].items():
                    try:
                        attr = self.MIME_MAP[mime]
                    except KeyError:
                        raise NotImplementedError('unhandled mime type: %s' % mime)

                    setattr(out, attr, data)
                #print(data, end='')
            elif msg_type == 'pyerr':
                out.ename = content['ename']
                out.evalue = content['evalue']
                out.traceback = content['traceback']

                #logging.error('\n'.join(content['traceback']))
            else:
                raise NotImplementedError('unhandled iopub message: %s' % msg_type)
            
            cell['outputs'].append(out)
            if autosave:
                self.save_notebook(autosave)
#.........这里部分代码省略.........
开发者ID:fatlotus,项目名称:runipy,代码行数:103,代码来源:notebook_runner.py


注:本文中的IPython.kernel.inprocess.manager.InProcessKernelManager.shutdown_kernel方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。