Firefox 中的 IPython Notebook 中是否有等效于 CTRL+C 来中断正在运行的单元格?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17561212/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there an equivalent to CTRL+C in IPython Notebook in Firefox to break cells that are running?
提问by wwwilliam
I've started to use the IPython Notebook and am enjoying it. Sometimes, I write buggy code that takes massive memory requirements or has an infinite loop. I find the "interrupt kernel" option sluggish or unreliable, and sometimes I have to restart the kernel, losing everything in memory.
我已经开始使用 IPython Notebook 并且很享受它。有时,我会编写需要大量内存或无限循环的错误代码。我发现“中断内核”选项缓慢或不可靠,有时我必须重新启动内核,从而丢失内存中的所有内容。
I also sometimes write scripts that cause OS X to run out of memory, and I have to do a hard reboot. I'm not 100% sure, but when I've written bugs like this before and ran Python in the terminal, I can usually CTRL+Cmy scripts.
我有时也会编写导致 OS X 内存不足的脚本,我必须进行硬重启。我不是 100% 确定,但是当我之前写过这样的错误并在终端中运行 Python 时,我通常可以CTRL+C我的脚本。
I am using the Anaconda distribution of IPython notebook with Firefox on Mac OS X.
我在 Mac OS X 上使用带有 Firefox 的 IPython notebook 的 Anaconda 发行版。
采纳答案by seaotternerd
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.
我可能是错的,但我很确定“中断内核”按钮只是向您当前正在运行的代码发送一个 SIGINT 信号(这个想法得到了费尔南多在这里的评论的支持),这与点击CTRL+C 就可以了。python 中的一些进程比其他进程更突然地处理 SIGINT。
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 Notebook 中运行的某些程序并从终端启动 iPython Notebook,则可以在该终端中按 CTRL+C 两次以中断整个 iPython Notebook 服务器。这将完全停止 iPython Notebook,这意味着无法重新启动或保存您的工作,因此这显然不是一个很好的解决方案(您需要按 CTRL+C 两次,因为它是一项安全功能,因此人们不会不小心这样做)。然而,在紧急情况下,它通常比“中断内核”按钮更快地杀死进程。
回答by cutie
You can press I
twice to interrupt the kernel.
你可以按I
两次来中断内核。
This only works if you're in Command mode. If not already enabled, press Escto enable it.
这仅在您处于命令模式时有效。如果尚未启用,请按Esc以启用它。
回答by Skuli
回答by tomp
To add to the above: If interrupt is not working, you can restart the kernel.
补充上述内容: 如果中断不起作用,您可以重新启动内核。
Go to the kernel dropdown >> restart >> restart and clear output. This usually does the trick. If this still doesn't work, kill the kernel in the terminal (or task manager) and then restart.
转到内核下拉菜单>>重新启动>>重新启动并清除输出。这通常可以解决问题。如果这仍然不起作用,请在终端(或任务管理器)中杀死内核,然后重新启动。
Interrupt doesn't work well for all processes. I especially have this problem using the R kernel.
中断不适用于所有进程。我在使用 R 内核时特别有这个问题。
回答by mbecker
UPDATE: Turned my solution into a stand-alone python script.
更新:将我的解决方案变成了一个独立的 python 脚本。
This solution has saved me more than once. Hopefully others find it useful. This python script will find any jupyter kernel using more than cpu_threshold
CPU and prompts the user to send a SIGINT
to the kernel (KeyboardInterrupt). It will keep sending SIGINT
until the kernel's cpu usage goes below cpu_threshold
. If there are multiple misbehaving kernels it will prompt the user to interrupt each of them (ordered by highest CPU usage to lowest). A big thanks goes to gcbeltraminifor writing code to find the name of a jupyter kernel using the jupyter api. This script was tested on MACOS with python3 and requires jupyter notebook, requests, json and psutil.
这个解决方案不止一次救了我。希望其他人觉得它有用。这个 python 脚本将找到任何使用超过cpu_threshold
CPU 的jupyter 内核,并提示用户向SIGINT
内核发送一个(KeyboardInterrupt)。它将继续发送,SIGINT
直到内核的 CPU 使用率低于cpu_threshold
。如果有多个行为不端的内核,它会提示用户中断每个内核(按 CPU 使用率从高到低排序)。非常感谢gcbeltramini编写代码以使用 jupyter api 查找 jupyter 内核的名称。这个脚本在 MACOS 上用 python3 测试过,需要 jupyter notebook、requests、json 和 psutil。
Put the script in your home directory and then usage looks like:
将脚本放在您的主目录中,然后用法如下:
python ~/interrupt_bad_kernels.py
Interrupt kernel chews cpu.ipynb; PID: 57588; CPU: 2.3%? (y/n) y
Script code below:
脚本代码如下:
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()