我正在编写一个带有单元魔术的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中,扩展可以处理内核中断吗?
发布评论