在Firefox中的IPython Notebook中是否有相当于CTRL + C来破坏正在运行的单元格?

www*_*iam 84 python ipython jupyter-notebook

我已经开始使用IPython笔记本了,我很享受.有时,我会编写需要大量内存或无限循环的错误代码.我发现"中断内核"选项缓慢或不可靠,有时我必须重新启动内核,丢失内存中的所有内容.

我有时也会写脚本导致OS X耗尽内存,我必须重新启动.我不是百分百肯定,但是当我之前写过这样的bug并在终端中运行Python时,我通常可以CTRL+ C我的脚本.

我在Mac OS X上使用Anaconda分发的IPython笔记本和Firefox.

小智 63

您可以按I两次以中断内核.

这仅在您处于命令模式时才有效.如果尚未启用,请按Esc启用它.


sea*_*erd 53

I could be wrong, but I'm pretty sure that the "interrupt kernel" button just sends a SIGINT signal to the code that you're currently running (this idea is supported by Fernando's comment here), which is the same thing that hitting CTRL+C would do. Some processes within python handle SIGINTs more abruptly than others.

If you desperately need to stop something that is running in iPython Notebook and you started iPython Notebook from a terminal, you can hit CTRL+C twice in that terminal to interrupt the entire iPython Notebook server. This will stop iPython Notebook alltogether, which means it won't be possible to restart or save your work, so this is obviously not a great solution (you need to hit CTRL+C twice because it's a safety feature so that people don't do it by accident). In case of emergency, however, it generally kills the process more quickly than the "interrupt kernel" button.

  • 或者,您可以重新启动或停止违规内核 - 不如杀死ipython服务器那么激烈.这可以通过`Kernel`下拉列表或从笔记本服务器的页面(违规笔记本名称右侧的`Shutdown`按钮)完成. (12认同)
  • 不幸的是,浏览器似乎变得如此无响应,以至于难以访问服务器页面。 (2认同)

Sku*_*uli 5

是IPython Notebook的快捷方式。

Ctrl-m i中断内核。(即后面的唯一字母i Ctrl-m

根据答案,I两次也可以。


tom*_*omp 5

补充上述内容: 如果中断不起作用,您可以重新启动内核。

转到内核下拉菜单>>重新启动>>重新启动并清除输出。这通常可以解决问题。如果这仍然不起作用,请在终端(或任务管理器)中杀死内核,然后重新启动。

中断不适用于所有进程。我在使用 R 内核时特别有这个问题。


mbe*_*ker 5

更新:将我的解决方案变成一个独立的 python 脚本。

这个解决方案不止一次地救了我。希望其他人觉得它有用。这个Python脚本将找到任何使用超过cpu_thresholdCPU的jupyter内核,并提示用户SIGINT向内核发送一个(键盘中断)。它将继续发送,SIGINT直到内核的 cpu 使用率低于cpu_threshold。如果有多个行为不当的内核,它将提示用户中断每个内核(按 CPU 使用率最高到最低的顺序排列)。非常感谢gcbeltramini编写代码以使用 jupyter api 查找 jupyter 内核的名称。该脚本在 MACOS 上使用 python3 进行了测试,需要 jupyter notebook、requests、json 和 psutil。

将脚本放在您的主目录中,然后用法如下:

python ~/interrupt_bad_kernels.py
Interrupt kernel chews cpu.ipynb; PID: 57588; CPU: 2.3%? (y/n) y
Run Code Online (Sandbox Code Playgroud)

脚本代码如下:

from os import getpid, kill
from time import sleep
import re
import signal

from notebook.notebookapp import list_running_servers
from requests import get
from requests.compat import urljoin
import ipykernel
import json
import psutil


def get_active_kernels(cpu_threshold):
    """Get a list of active jupyter kernels."""
    active_kernels = []
    pids = psutil.pids()
    my_pid = getpid()

    for pid in pids:
        if pid == my_pid:
            continue
        try:
            p = psutil.Process(pid)
            cmd = p.cmdline()
            for arg in cmd:
                if arg.count('ipykernel'):
                    cpu = p.cpu_percent(interval=0.1)
                    if cpu > cpu_threshold:
                        active_kernels.append((cpu, pid, cmd))
        except psutil.AccessDenied:
            continue
    return active_kernels


def interrupt_bad_notebooks(cpu_threshold=0.2):
    """Interrupt active jupyter kernels. Prompts the user for each kernel."""

    active_kernels = sorted(get_active_kernels(cpu_threshold), reverse=True)

    servers = list_running_servers()
    for ss in servers:
        response = get(urljoin(ss['url'].replace('localhost', '127.0.0.1'), 'api/sessions'),
                       params={'token': ss.get('token', '')})
        for nn in json.loads(response.text):
            for kernel in active_kernels:
                for arg in kernel[-1]:
                    if arg.count(nn['kernel']['id']):
                        pid = kernel[1]
                        cpu = kernel[0]
                        interrupt = input(
                            'Interrupt kernel {}; PID: {}; CPU: {}%? (y/n) '.format(nn['notebook']['path'], pid, cpu))
                        if interrupt.lower() == 'y':
                            p = psutil.Process(pid)
                            while p.cpu_percent(interval=0.1) > cpu_threshold:
                                kill(pid, signal.SIGINT)
                                sleep(0.5)

if __name__ == '__main__':
    interrupt_bad_notebooks()
Run Code Online (Sandbox Code Playgroud)