栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

连接到远程IPython实例

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

连接到远程IPython实例

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

要点:

  • ipython内核在中编写JSON连接文件,
    IPYTHONDIR/profile_<name>/security/kernel-<id>.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_filefrom 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 thatcf = find_connection_file('6759')km = BlockingKernelManager(connection_file=cf)# load connection info and init communicationkm.load_connection_file()km.start_channels()def run_cell(km, pre):    # now we can run pre.  This is done on the shell channel    shell = km.shell_channel    print    print "running:"    print pre    # execution is immediate and async, returning a UUID    msg_id = shell.execute(pre)    # 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 linerun_cell(km, 'a=5')run_cell(km, 'b=0')run_cell(km, 'c=a/b')

运行的输出:

running:a=5succeeded!running:b=0succeeded!running:c=a/bfailed!---------------------------------------------------------------------------ZeroDivisionError   Traceback (most recent call last)/Users/minrk/<ipython-input-11-fb3f79bd285b> in <module>()----> 1 c=a/bZeroDivisionError: integer division or modulo by zero

有关如何解释回复的更多信息,请参见消息规范。如果相关,则stdout /
err和display数据将过来

km.iopub_channel
,您可以使用返回的msg_id将
shell.execute()
输出与给定执行关联。

PS:对于这些新功能的文档质量,我深表歉意。我们有很多工作要做。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/617649.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号