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


Python KernelManager.write_connection_file方法代码示例

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


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

示例1: ExecutePreprocessor

# 需要导入模块: from IPython.kernel import KernelManager [as 别名]
# 或者: from IPython.kernel.KernelManager import write_connection_file [as 别名]
class ExecutePreprocessor(Preprocessor):
    """
    Executes all the cells in a notebook
    """
    
    timeout = Integer(30, config=True,
        help="The time to wait (in seconds) for output from executions."
    )
    # FIXME: to be removed with nbformat v4
    # map msg_type to v3 output_type
    msg_type_map = {
        "error" : "pyerr",
        "execute_result" : "pyout",
    }
    
    # FIXME: to be removed with nbformat v4
    # map mime-type to v3 mime-type keys
    mime_map = {
        "text/plain" : "text",
        "text/html" : "html",
        "image/svg+xml" : "svg",
        "image/png" : "png",
        "image/jpeg" : "jpeg",
        "text/latex" : "latex",
        "application/json" : "json",
        "application/javascript" : "javascript",
    }
    
    extra_arguments = List(Unicode)
    
    def _create_client(self):
        from IPython.kernel import KernelManager
        self.km = KernelManager()
        self.km.write_connection_file()
        self.kc = self.km.client()
        self.kc.start_channels()
        self.km.start_kernel(extra_arguments=self.extra_arguments, stderr=open(os.devnull, 'w'))
        self.iopub = self.kc.iopub_channel
        self.shell = self.kc.shell_channel
        self.shell.kernel_info()
        try:
            self.shell.get_msg(timeout=self.timeout)
        except Empty:
            self.log.error("Timeout waiting for kernel_info reply")
            raise
        # flush IOPub
        while True:
            try:
                self.iopub.get_msg(block=True, timeout=0.25)
            except Empty:
                break

    def _shutdown_client(self):
        self.kc.stop_channels()
        self.km.shutdown_kernel()
        del self.km

    def preprocess(self, nb, resources):
        self._create_client()
        nb, resources = super(ExecutePreprocessor, self).preprocess(nb, resources)
        self._shutdown_client()
        return nb, resources

    def preprocess_cell(self, cell, resources, cell_index):
        """
        Apply a transformation on each code cell. See base.py for details.
        """
        if cell.cell_type != 'code':
            return cell, resources
        try:
            outputs = self.run_cell(self.shell, self.iopub, cell)
        except Exception as e:
            self.log.error("failed to run cell: " + repr(e))
            self.log.error(str(cell.input))
            raise
        cell.outputs = outputs
        return cell, resources

    def run_cell(self, shell, iopub, cell):
        msg_id = shell.execute(cell.input)
        self.log.debug("Executing cell:\n%s", cell.input)
        # wait for finish, with timeout
        while True:
            try:
                msg = shell.get_msg(timeout=self.timeout)
            except Empty:
                self.log.error("Timeout waiting for execute reply")
                raise
            if msg['parent_header'].get('msg_id') == msg_id:
                break
            else:
                # not our reply
                continue
        
        outs = []

        while True:
            try:
                msg = iopub.get_msg(timeout=self.timeout)
            except Empty:
#.........这里部分代码省略.........
开发者ID:Agent74,项目名称:ipython,代码行数:103,代码来源:execute.py


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