python - 通过 jupyter_client API 优雅地中断 jupyter 内核
问题描述
jupyter qtconsole
我的目标是使用API连接到运行jupyter_client
并在该内核上远程执行命令。代码的执行工作,但似乎不可能使用Ctrl-C
(在qtconsole
或console
)从内核前端中断正在运行的代码。在我的示例代码下面,我在不同的实例中执行qtconsole
并且不能被中断:
import jupyter_client as jc
# Find last accessed connection file
cfile = jc.find_connection_file()
km = jc.KernelManager()
km.load_connection_file(connection_file=cfile)
kc.start_channels()
msg = """
import time
for k in range(10):
print(k)
time.sleep(1)
"""
kc.execute(msg)
我发现中断执行的唯一方法是直接发送一个SIGINIT
,例如通过在另一个jupyter
实例中运行以下命令:
import os
import signal
# pid is process identifier of jupyter kernel that runs the loop
os.kill(pid, signal.SIGINT)
问题:有没有更优雅的方式使用jupyter_client
API(例如KernelClient
或KernelManager
)中断正在运行的内核?
PS:github 上还有一个 open issue提到了这个问题,但是 jupyter 开发人员到目前为止还不是很敏感。
解决方案
推荐阅读
- google-chrome - 用于批量域的 Web 浏览器插件
- javascript - 将本机脉冲图像反应为 instagram
- amazon-web-services - 在 AWS Glue 中读写 MYSQL
- javascript - 是否有 HTML5 验证 API 将元素的状态设置为无效?
- java - 如何获得在 Java 中实现特定接口的正确枚举?
- angular - 将员工电话从表格添加到表格
- mysql - 如何在 LeftJoin 中添加附加参数?
- regex - .htaccess 论坛软件迁移后的重定向规则
- webrtc - webrtc连接STUN时,STUN返回的公网IP端口是否与webrtc create socket with STUN使用的端口相同?
- php - Wordpress / Shortcodes:Wordpress 删除的 html 标记