连接到远程IPython实例

编程入门 行业动态 更新时间:2024-10-24 22:24:10
本文介绍了连接到远程IPython实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

I would like to run an IPython instance on one machine and connect to it (over LAN) from a different process (to run some python commands). I understand that it is possible with zmq : ipython/ipython-doc/dev/development/ipythonzmq.html .

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

However, I can not find documentation on how to do it and whether it is even possible yet.

任何帮助将不胜感激!

编辑

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

I would like to be able to connect to IPython kernel instance and send it python commands. However, this should not be done via a graphic tool (qtconsole) , but I want to be able to connect to that kernel instance from within a different python script...

例如

somehow_connect_to_ipython_kernel_instance instance.run_command("a=6")

推荐答案

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

If you want to run code in a kernel from another Python program, the easiest way is to connect a BlockingKernelManager. The best example of this right now is Paul Ivanov's vim-ipython client, or IPython's own terminal client.

要点:

  • ipython内核写入JSON连接文件,在 IPYTHONDIR / profile_< name> / security / kernel-< id> .json ,其中包含各种客户端连接和执行代码所需的信息。
  • KernelManagers是用于与内核通信的对象(执行代码) ,收到结果等)。 *
  • ipython kernels write JSON connection files, in IPYTHONDIR/profile_<name>/security/kernel-<id>.json, which contain information necessary for various clients to connect and execute code.
  • KernelManagers are the objects that are used to communicate with kernels (execute code, receive results, etc.). *

一个工作示例:

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

In a shell, do ipython kernel (or ipython qtconsole, if you want to share a kernel with an already running GUI):

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

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

This wrote the 'kernel-6759.json' file

然后你可以运行这个Python片段连接一个KernelManager ,并运行一些代码:

Then you can run this Python snippet to connect a KernelManager, and run some code:

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

有关如何解释回复的详细信息,请参阅消息规范。如果相关,stdout / err和显示数据将超过 km.iopub_channel ,并且您可以使用返回的msg_id shell.execute()将输出与给定的执行相关联。

see the message spec for more information on how to interpret the reply. If relevant, stdout/err and display data will come over km.iopub_channel, and you can use the msg_id returned by shell.execute() to associate output with a given execution.

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

PS: I apologize for the quality of the documentation of these new features. We have a lot of writing to do.

更多推荐

连接到远程IPython实例

本文发布于:2023-11-23 21:07:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1622862.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:连接到   实例   IPython

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!