在python内核中运行子内核

时间:2017-04-05 17:41:06

标签: python ipython zeromq jupyter-notebook ipython-magic

我正在尝试将groovy内核作为python中的子内核运行。我在python中添加了自定义单元格魔术,因此当用户类型%%groovy groovy内核应该使用jupyter_client管理器启动并执行代码时。目前,我在发送回复时遇到问题。任何帮助表示赞赏。 这是我目前的代码:

from IPython.core.magic import (Magics, magics_class,cell_magic)
from jupyter_client import manager
from ipykernel.kernelbase import Kernel

@magics_class
class GroovyKernelMagic(Magics,Kernel):
    _execution_count = 1

    def run_cell(self, code):
        if not self.KernelManager.is_alive():
            Kernel.send_response(Kernel.iopub_socket, 'stream',
                               {'name': 'stdout', 'text': 'Restarting kernel "{}"\n'.format(self.KernelManager)})
            self.KernelManager.restart_kernel(now=False)
            self.KernelClient = self.KernelManager.client()
        while self.KernelClient.shell_channel.msg_ready():
            self.KernelClient.shell_channel.get_msg()
        self.KernelClient.execute(code, silent=False)

        _execution_state = "busy"
        while _execution_state != 'idle':
            while self.KernelClient.iopub_channel.msg_ready():
                sub_msg = self.KernelClient.iopub_channel.get_msg()
                msg_type = sub_msg['header']['msg_type']
                if msg_type == 'status':
                    _execution_state = sub_msg["content"]["execution_state"]
                else:
                    if msg_type in ('execute_input', 'execute_result'):
                        sub_msg['content']['execution_count'] = self._execution_count

                    Kernel.send_response(Kernel.iopub_socket, msg_type, sub_msg['content'])
        reply = self.KernelClient.get_shell_msg(timeout=10)
        reply['content']['execution_count'] = self._execution_count
        return reply['content']

    @cell_magic
    def groovy(self, line, cell):
        self.KernelManager, self.KernelClient = manager.start_new_kernel(startup_timeout=60, kernel_name='groovy')
        return self.run_cell(cell)


ip = get_ipython()
ip.register_magics(GroovyKernelMagic)

这是错误堆栈跟踪:

/testMagic.py in groovy(self,line,cell)      50 def groovy(self,line,cell):      51 self.KM,self.KC = manager.start_new_kernel(startup_timeout = 60,kernel_name ='groovy') ---> 52返回self.run_cell(cell)      53      54

run_cell中的

/testMagic.py(self,code)      42打印(msg_type)      43打印(sub_msg ['content']) ---> 44 Kernel.send_response(Kernel.iopub_socket,msg_type,sub_msg ['content'])      45 reply = self.KC.get_shell_msg(timeout = 10)      46回复['content'] ['execution_count'] = self._execution_count

send_response中的 anaconda3 / lib / python3.5 / site-packages / ipykernel / kernelbase.py(self,stream,msg_or_type,content,ident,buffers,track,header,metadata)     343消息。     344“”“ - > 345返回self.session.send(stream,msg_or_type,content,self._parent_header,     346个身份,缓冲区,轨道,标题,元数据)     347

AttributeError:'任何'对象没有属性'session'

0 个答案:

没有答案