在IPython中,扩展可以处理内核中断吗? [英] In IPython, can extensions handle kernel interrupts?

查看:62
本文介绍了在IPython中,扩展可以处理内核中断吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个带有单元魔术的IPython扩展,该魔术通过pexpect调用另一个可执行文件.在内核生命周期内,它将使该可执行文件在后台运行.

I'm writing an IPython extension with cell magics that call out to another executable via pexpect. It keeps this executable running in the background for the life of the kernel. Is there a hook somewhere so that I can send this subprocess Ctrl-C when a kernel interrupt is raised (eg, the "Interrupt Kernel" menu option in the IPython Notebook)?

推荐答案

重新发布作为答案:

IPython通过发送SIGINT中断内核,该信号与在终端中按Ctrl-C时触发的信号相同.因此,只要您想在运行自己的代码时捕获它,就可以捕获KeyboardInterrupt,就像这样:

IPython interrupts the kernel by sending a SIGINT, the same signal that's fired when you press Ctrl-C in a terminal. So, so long as you want to catch it while your own code is running, you can just catch KeyboardInterrupt, like this:

p.sendline('some command')
try:
    p.expect(processing_finished_mark)
except KeyboardInterrupt:
    p.sendintr()

这篇关于在IPython中,扩展可以处理内核中断吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆