在IPython中,扩展可以处理内核中断吗?

编程入门 行业动态 更新时间:2024-10-25 02:21:31
本文介绍了在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中,扩展可以处理内核中断吗?

本文发布于:2023-07-16 23:00:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1126833.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:内核   IPython

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!