acfoltzer
acfoltzer

Reputation: 5608

In IPython, can extensions handle kernel interrupts?

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)?

Upvotes: 1

Views: 674

Answers (1)

Thomas K
Thomas K

Reputation: 40340

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()

Upvotes: 2

Related Questions