连接到远程IPython实例

问题描述 投票:27回答:5

我想在一台机器上运行一个IPython实例,并从另一个进程(通过LAN)连接到它(运行一些python命令)。我知道zmq:http://ipython.org/ipython-doc/dev/development/ipythonzmq.html是可能的。

但是,我找不到有关如何操作的文档以及是否可能。

任何帮助,将不胜感激!


编辑

我希望能够连接到IPython内核实例并发送它python命令。但是,这不应该通过图形工具(qtconsole)完成,但我希望能够从不同的python脚本中连接到该内核实例...

EG

external.朋友

somehow_connect_to_ipython_kernel_instance
instance.run_command("a=6")
python ipython zeromq jupyter pyzmq
5个回答
29
投票

如果你想从另一个Python程序在内核中运行代码,最简单的方法是连接BlockingKernelManager。现在最好的例子是Paul Ivanov的vim-ipython客户端,或者IPython自己的terminal client

要旨:

  • ipython内核在IPYTHONDIR/profile_<name>/security/kernel-<id>.json中编写JSON连接文件,其中包含各种客户端连接和执行代码所需的信息。
  • KernelManagers是用于与内核通信的对象(执行代码,接收结果等)。 *

一个工作的例子:

在shell中,执行ipython kernel(或ipython qtconsole,如果要与已运行的GUI共享内核):

$> ipython kernel
[IPKernelApp] To connect another client to this kernel, use:
[IPKernelApp] --existing kernel-6759.json

这写了'kernel-6759.json'文件

然后,您可以运行此Python代码段来连接KernelManager,并运行一些代码:

from IPython.lib.kernel import find_connection_file
from IPython.zmq.blockingkernelmanager import BlockingKernelManager

# this is a helper method for turning a fraction of a connection-file name
# into a full path.  If you already know the full path, you can just use that
cf = find_connection_file('6759')

km = BlockingKernelManager(connection_file=cf)
# load connection info and init communication
km.load_connection_file()
km.start_channels()

def run_cell(km, code):
    # now we can run code.  This is done on the shell channel
    shell = km.shell_channel
    print
    print "running:"
    print code

    # execution is immediate and async, returning a UUID
    msg_id = shell.execute(code)
    # get_msg can block for a reply
    reply = shell.get_msg()

    status = reply['content']['status']
    if status == 'ok':
        print 'succeeded!'
    elif status == 'error':
        print 'failed!'
        for line in reply['content']['traceback']:
            print line

run_cell(km, 'a=5')
run_cell(km, 'b=0')
run_cell(km, 'c=a/b')

运行的输出:

running:
a=5
succeeded!

running:
b=0
succeeded!

running:
c=a/b
failed!
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
/Users/minrk/<ipython-input-11-fb3f79bd285b> in <module>()
----> 1 c=a/b

ZeroDivisionError: integer division or modulo by zero

有关如何解释回复的更多信息,请参阅message spec。如果相关,stdout / err和显示数据将通过km.iopub_channel,您可以使用shell.execute()返回的msg_id将输出与给定的执行相关联。

PS:我为这些新功能的文档质量道歉。我们有很多写作要做。


22
投票

如果您只想以交互方式连接,则可以使用SSH转发。我还没有在Stack Overflow上的任何地方找到这个,但这个问题最接近。这个答案已在Ipython 0.13上测试过。我从this blog post获得了信息。

  1. 在远程计算机上运行ipython kerneluser@remote:~$ ipython3 kernel [IPKernelApp] To connect another client to this kernel, use: [IPKernelApp] --existing kernel-25333.json
  2. 看看kernel-25333.json文件: user@remote:~$ cat ~/.ipython/profile_default/security/kernel-25333.json { "stdin_port": 54985, "ip": "127.0.0.1", "hb_port": 50266, "key": "da9c7ae2-02aa-47d4-8e67-e6153eb15366", "shell_port": 50378, "iopub_port": 49981 }
  3. 在本地计算机上设置端口转发: user@local:~$ ssh user@remote -f -N -L 54985:127.0.0.1:54985 user@local:~$ ssh user@remote -f -N -L 50266:127.0.0.1:50266 user@local:~$ ssh user@remote -f -N -L 50378:127.0.0.1:50378 user@local:~$ ssh user@remote -f -N -L 49981:127.0.0.1:49981
  4. kernel-25333.json文件复制到本地计算机: user@local:~$ rsync -av user@remote:.ipython/profile_default/security/kernel-25333.json ~/.ipython/profile_default/security/kernel-25333.json
  5. 使用新内核在本地计算机上运行ipython: user@local:~$ ipython3 console --existing kernel-25333.json Python 3.2.3 (default, Oct 19 2012, 19:53:16) Type "copyright", "credits" or "license" for more information. IPython 0.13.1.rc2 -- An enhanced Interactive Python. ? -> Introduction and overview of IPython's features. %quickref -> Quick reference. help -> Python's own help system. object? -> Details about 'object', use 'object??' for extra details. In [1]: import socket; print(socket.gethostname()) remote

13
投票

分手到jupyter后更新到minrk的答案。使用jupyter_client(4.1.1),最简单的代码就像:

import jupyter_client

cf=jupyter_client.find_connection_file('6759')
km=jupyter_client.BlockingKernelClient(connection_file=cf)
km.load_connection_file()

km.execute('a=5')

注意:

  • jupyter_client.BlockingKernelClient也与jupyter_client.client.BlockingKernelClient别名。
  • shell(km.shell_channel)不再具有execute()和get_msg()方法。

目前很难找到更新的文件;还没有在http://jupyter-client.readthedocs.org/en/latest/上用于BlockingKernelClient。 https://github.com/jupyter/jupyter_kernel_test中的一些代码。欢迎任何链接。


3
投票

以上答案有点旧。最新版本的ipython的解决方案要简单得多,但在一个地方没有很好的记录。所以我想我会在这里记录下来。

Solution to connect from any OS to a ipython kernel running on Windows

如果客户端或服务器是linux或其他操作系统,只需根据kernel-1234.json适当更改Where is kernel-1234.json located in Jupyter under Windows?的位置

  1. 在基于Windows的内核启动时,请确保使用ipykernel安装pip install ipykernel
  2. 使用ipykernel启动ipython kernel -f kernel-1234.json
  3. kernel-1234.json机器上找到Windows文件。该文件可能有一个不同的数字,而不是1234,很可能位于'C:\ Users \ me \ AppData \ Roaming \ jupyter \ runtime \ kernel-1234.json':https://stackoverflow.com/a/48332006/4752883
  4. 使用pip install jupyter-consolepip install qtconsole https://jupyter-console.readthedocs.io/en/latest/安装Jupyter Console(或Jupyter Qtconsole / notebook)
  5. 如果您在Windows上,请执行ipconfig以查找Windows服务器的IP地址。 (在Linux上,在shell提示符下执行ifconfig)。在kernel-1234.json文件中,将IP地址从127.0.0.1更改为服务器的IP地址。如果您从另一个Windows服务器连接,然后将kernel-1234.json文件复制到您的本地计算机并记下该路径。
  6. 导航到包含kernel-1234.json的文件夹,并使用jupyter console --existing kernel-1234.json启动Jupyter Console

1
投票

如果您使用的是Anaconda,则在OS X中存储JSON文件

/用户/ [用户名] /资源库/ Jupyter /运行/

在Windows中:

C:\用户[用户名] \应用程序数据\漫游\ jupyter \运行\

© www.soinside.com 2019 - 2024. All rights reserved.