【问题标题】:Jupyter Client Connect to Running Kernel via PythonJupyter 客户端通过 Python 连接到正在运行的内核
【发布时间】:2018-01-05 22:27:08
【问题描述】:

我正在尝试以编程方式(使用 Python)与我正在运行的 jupyter 内核进行交互,作为原型设计实验。

我的浏览器中运行着一个 jupyter notebook,我通过魔法命令从 notebook 获取了连接信息

%connect_info
{
 "signature_scheme": "hmac-sha256",
 "shell_port": 49545,
 "kernel_name": "",
 "iopub_port": 49546,
 "stdin_port": 49547,
 "hb_port": 49549,
 "control_port": 49548,
 "key": "1a359267-f30d84302c39d352d6ac17c3",
 "transport": "tcp",
 "ip": "127.0.0.1"
}

KernelClient 类的jupyter_client docs 让我相信我可以连接到此信息并使用此对象收听/显示代码以及发送要通过我连接到的内核执行的代码,但是,我没有我一直在尝试的任何运气,例如:

>>> from jupyter_client import KernelClient
>>> connection = {
  "signature_scheme": "hmac-sha256",
  "shell_port": 49545,
  "kernel_name": "",
  "iopub_port": 49546,
  "stdin_port": 49547,
  "hb_port": 49549,
  "control_port": 49548,
  "key": "1a359267-f30d84302c39d352d6ac17c3",
  "transport": "tcp",
  "ip": "127.0.0.1"
}
>>> client = KernelClient(**connection)
>>> client.history()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/path/to/envs/jupyter-nb/lib/python3.5/site-packages/jupyter_client/client.py", line 347, in history
self.shell_channel.send(msg)
  File "/path/to/envs/jupyter-nb/lib/python3.5/site-packages/jupyter_client/client.py", line 143, in shell_channel
socket, self.session, self.ioloop
>>> client.start_channels()
  Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/path/to/jupyter-nb/lib/python3.5/site-packages/jupyter_client/client.py", line 101, in start_channels
self.shell_channel.start()
  File "/path/to/jupyter-nb/lib/python3.5/site-packages/jupyter_client/client.py", line 143, in shell_channel
socket, self.session, self.ioloop
 TypeError: object() takes no parameters
 >>> client.load_connection_info(connection)
 >>> client.execute('print("Hello World")')
   Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
   File "/path/to/jupyter-nb/lib/python3.5/site-packages/jupyter_client/client.py", line 254, in execute
self.shell_channel.send(msg)
   File "/path/to/jupyter-nb/lib/python3.5/site-packages/jupyter_client/client.py", line 143, in shell_channel
socket, self.session, self.ioloop
   TypeError: object() takes no parameters

编辑:考虑到@Sam H. 的建议,为了清楚起见添加此部分,但它没有用

>>> from jupyter_client import KernelClient
>>> kc = KernelClient()
>>> kc.load_connection_info(connection)
>>> kc.start_channels()
  Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/path/to/jupyter-nb/lib/python3.5/site-packages/jupyter_client/client.py", line 101, in start_channels
  self.shell_channel.start()
  File "/path/to/jupyter-nb/lib/python3.5/site-packages/jupyter_client/client.py", line 143, in shell_channel
  socket, self.session, self.ioloop
  TypeError: object() takes no parameters

【问题讨论】:

    标签: python ipython jupyter-notebook jupyter


    【解决方案1】:

    我过去玩过KernelClient,取得了一点点成功,诚然认为成功率非常低。我让事情工作的唯一方法是通过KernelManager(你没有)。例如尝试:

    from jupyter_client import KernelManager
    km = KernelManager()
    km.start_kernel()
    kc = km.client()
    # now execute something in the client
    kc.execute("2+2")
    while True:
        try:
            kc_msg = kc.get_iopub_msg(timeout=1)
            if 'content' in kc_msg and 'data' in kc_msg['content']:
                print('the kernel produced data {}'.format(kc_msg['content']['data']))
                break        
        except:
            print('timeout kc.get_iopub_msg')
            pass
    

    通常(但不总是)返回:

    the kernel produced data {'text/plain': '4'}
    

    我做了一个快速的谷歌搜索,我发现唯一一个表明我走在正确轨道上的代码或 SO 帖子是:This post

    注意我知道这绝不是一个完整的答案,但希望这是朝着正确方向迈出的一步。如果你成功了,我会很高兴学习。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-10-18
      • 2017-05-08
      • 2021-09-24
      • 2018-01-06
      • 2016-06-13
      • 1970-01-01
      • 2010-09-17
      相关资源
      最近更新 更多