Oha*_*had 27 python ipython zeromq pyzmq jupyter
我想在一台机器上运行一个IPython实例,并从另一个进程(通过LAN)连接到它(运行一些python命令).我知道有可能使用zmq:http://ipython.org/ipython-doc/dev/development/ipythonzmq.html.
但是,我找不到有关如何操作的文档以及是否可能.
任何帮助,将不胜感激!
编辑
我希望能够连接到IPython内核实例并发送它python命令.但是,这不应该通过图形工具(qtconsole)完成,但我希望能够从不同的python脚本中连接到该内核实例...
例如
somehow_connect_to_ipython_kernel_instance
instance.run_command("a=6")
Run Code Online (Sandbox Code Playgroud)
min*_*nrk 29
如果要从另一个Python程序在内核中运行代码,最简单的方法是连接BlockingKernelManager.现在最好的例子是Paul Ivanov的vim-ipython客户端,或者IPython自己的终端客户端.
要旨:
IPYTHONDIR/profile_<name>/security/kernel-<id>.json其中包含各种客户端连接和执行代码所需的信息.一个工作的例子:
在shell中,执行ipython kernel(或者ipython qtconsole,如果要与已经运行的GUI共享内核):
$> ipython kernel
[IPKernelApp] To connect another client to this kernel, use:
[IPKernelApp] --existing kernel-6759.json
Run Code Online (Sandbox Code Playgroud)
这写了'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')
Run Code Online (Sandbox Code Playgroud)
运行的输出:
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
Run Code Online (Sandbox Code Playgroud)
有关如何解释回复的更多信息,请参阅消息规范.如果相关,stdout/err和显示数据将会过来km.iopub_channel,您可以使用返回的msg_id将shell.execute()输出与给定的执行相关联.
PS:我为这些新功能的文档质量道歉.我们有很多写作要做.
ger*_*rit 22
如果您只想以交互方式连接,则可以使用SSH转发.我还没有在Stack Overflow上的任何地方找到这个,但这个问题最接近.这个答案已在Ipython 0.13上测试过.我从这篇博文中得到了这些信息.
ipython kernel在远程计算机上运行:
user@remote:~$ ipython3 kernel
[IPKernelApp] To connect another client to this kernel, use:
[IPKernelApp] --existing kernel-25333.json
Run Code Online (Sandbox Code Playgroud)看看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
}
Run Code Online (Sandbox Code Playgroud)在本地计算机上设置端口转发:
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
Run Code Online (Sandbox Code Playgroud)将kernel-25333.json文件复制到本地计算机:
user@local:~$ rsync -av user@remote:.ipython/profile_default/security/kernel-25333.json ~/.ipython/profile_default/security/kernel-25333.json
Run Code Online (Sandbox Code Playgroud)使用新内核在本地计算机上运行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
Run Code Online (Sandbox Code Playgroud)小智 14
分手到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')
Run Code Online (Sandbox Code Playgroud)
注意:
目前很难找到更新的文件; 还没有http://jupyter-client.readthedocs.org/en/latest/用于BlockingKernelClient.https://github.com/jupyter/jupyter_kernel_test中的一些代码.欢迎任何链接.
上面的答案有点旧了。最新版本的解决方案ipython要简单得多,但在一处没有很好的记录。所以我想我应该在这里记录它。
Windows如果客户端或服务器是一个或其他操作系统,只需根据Where is kernel-1234.json located in Jupyter under Windows?linux适当更改位置即可。kernel-1234.json
ipykernel使用以下命令安装pip install ipykernelipykernel使用ipython kernel -f kernel-1234.jsonkernel-1234.json在您的计算机上找到该文件Windows。该文件可能会有不同的编号,而不是1234并且很可能位于“C:\Users\me\AppData\Roaming\jupyter\runtime\kernel-1234.json”中: https: //stackoverflow.com/a/ 48332006/4752883pip install jupyter-console使用https://jupyter-console.readthedocs.io/en/latest/安装 Jupyter Console(或 Jupyter Qtconsole/notebook)pip install qtconsole ipconfig查找 Windows 服务器的 IP 地址。(在 Linux 上,ifconfig在 shell 提示符处执行 a)。在kernel-1234.json文件中将 IP 地址更改为127.0.0.1您服务器的 IP 地址。如果您从其他服务器进行连接Windows,请将文件复制kernel-1234.json到本地计算机并记下路径。kernel-1234.json启动Jupyter Consolejupyter console --existing kernel-1234.json| 归档时间: |
|
| 查看次数: |
22434 次 |
| 最近记录: |