1

我正在编写一个带有单元魔法的 IPython 扩展,它通过调用另一个可执行文件pexpect。它使这个可执行文件在内核的整个生命周期中都在后台运行。是否有一个钩子,以便我可以Ctrl-C在引发内核中断时发送这个子进程(例如,IPython Notebook 中的“中断内核”菜单选项)?

4

1 回答 1

2

Reposting as an answer:

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()
于 2013-09-26T23:50:33.763 回答