简体   繁体   English

如何正确关闭 IPython Notebook?

[英]How to close IPython Notebook properly?

How to close IPython Notebook properly?如何正确关闭 IPython Notebook?

Currently, I just close the browser tabs and then use Ctrl+C in the terminal.目前,我只是关闭浏览器选项卡,然后在终端中使用Ctrl+C
Unfortunately, neither exit() nor ticking Kill kernel upon exit does help (they do kill the kernel they but don't exit the iPython).不幸的是,无论是exit()还是Kill kernel upon exit都没有帮助(他们确实杀死了 kernel 但不退出 iPython)。

There isn't currently a better way to do it than Ctrl+C in the terminal.目前没有比终端中的 Ctrl+C 更好的方法了。

We're thinking about how to have an explicit shutdown, but there's some tension between the notebook as a single-user application, where the user is free to stop it, and as a multi-user server, where only an admin should be able to stop it.我们正在考虑如何显式关闭,但是笔记本作为单用户应用程序(用户可以自由停止它)和多用户服务器(只有管理员应该能够)之间存在一些紧张关系阻止它。 We haven't quite worked out how to handle the differences yet.我们还没有完全弄清楚如何处理这些差异。

(For future readers, this is the situation with 0.12 released and 0.13 in development.) (对于未来的读者,这是 0.12 发布和 0.13 开发中的情况。)

Update December 2017 2017 年 12 月更新

The IPython Notebook has become the Jupyter Notebook. IPython Notebook 已成为 Jupyter Notebook。 A recent version has added a jupyter notebook stop shell command which will shut down a server running on that system.最近的版本添加了一个jupyter notebook stop shell 命令,该命令将关闭在该系统上运行的服务器。 You can pass the port number at the command line if it's not the default port 8888.如果不是默认端口 8888,您可以在命令行中传递端口号。

You can also use nbmanager , a desktop application which can show running servers and shut them down.您还可以使用nbmanager ,这是一个桌面应用程序,可以显示正在运行的服务器并关闭它们。

Finally, we are working on adding:最后,我们正在努力添加:

  • A config option to automatically shut down the server if you don't use it for a specified time.如果您在指定时间内不使用服务器,则自动关闭服务器的配置选项。
  • A button in the user interface to shut the server down.用户界面中用于关闭服务器的按钮。 (We know it's a bit crazy that it has taken this long. Changing UI is controversial.) (我们知道花了这么长时间有点疯狂。更改 UI 是有争议的。)

If you run jupyter in the background like me:如果你像我一样在后台运行 jupyter:

jupyter notebook &> /dev/null &

Then to exit jupyter completely, instead of Ctl-C, make an alias command:然后要完全退出 jupyter,而不是 Ctl-C,请创建别名命令:

echo 'alias quitjupyter="kill $(pgrep jupyter)"' >> ~/.bashrc

Restart your terminal.重启你的终端。 Kill all jupyter instances:杀死所有 jupyter 实例:

quitjupyter

Note: use double quotes inside of single quotes as shown above.注意:在单引号内使用双引号,如上所示。 The other way around will evaluate the expression before writing it to your .bashrc (you want to write the command itself not 'kill 1430' or whatever process number may be associated with a current jupyter instance).另一种方式将在将表达式写入您的 .bashrc 之前对其进行评估(您想编写命令本身而不是“kill 1430”或任何可能与当前 jupyter 实例相关联的进程号)。 Of course you can use any alias you wish.当然,您可以使用任何您希望的别名。 I actually use 'qjup':我实际上使用'qjup':

echo 'alias qjup="kill $(pgrep jupyter)"' >> ~/.bashrc

Restart your terminal.重启你的终端。 Kill all jupyter instances:杀死所有 jupyter 实例:

qjup

I think accepted answer outdated and is not valid anymore.我认为接受的答案已经过时,不再有效。

You can terminate jupyter notebook from web interface on file menü item.您可以在文件菜单项上从 Web 界面终止 jupyter notebook。

在此处输入图片说明

When you move Mouse cursor on "close and halt", you will see following explanation.当您将鼠标光标移动到“关闭并暂停”上时,您将看到以下说明。

在此处输入图片说明

And when you click "close and halt", you will see following message on terminal screen.当您单击“关闭并暂停”时,您将在终端屏幕上看到以下消息。

在此处输入图片说明

First step is to save all open notebooks.第一步是保存所有打开的笔记本。 And then think about shutting down your running Jupyter Notebook.然后考虑关闭正在运行的 Jupyter Notebook。 You can use this simple command:您可以使用这个简单的命令:

$ jupyter notebook stop 
Shutting down server on port 8888 ...

Which also takes the port number as argument and you can shut down the jupyter notebook gracefully.这也将端口号作为参数,您可以优雅地关闭 jupyter notebook。

For eg:例如:

jupyter notebook stop 8889 
Shutting down server on port 8889 ...

Additionally to know your current jupyter instance running, check below command:此外,要了解您当前正在运行的 jupyter 实例,请查看以下命令:

shell> jupyter notebook list 
Currently running servers:
http://localhost:8888/?token=ef12021898c435f865ec706de98632 :: /Users/username/jupyter-notebooks [/code]

These commands worked for me:这些命令对我有用:

jupyter notebook list # shows the running notebooks and their port-numbers
                      # (for instance: 8080)
lsof -n -i4TCP:[port-number] # shows PID.
kill -9 [PID] # kill the process.

This answer was adapted from here .这个答案是从这里改编的。

如果没有其他工作,请尝试从任务管理器(如果是 Windows)中终止 pythonw 进程。

Linux (Ubuntu 14.04)

As mentioned, try to kill ipython notebook processes properly by first going to the "running" tab in your ipynb/jupyter browser session, and then check open terminals on your console and shut down with ctrl-c.如前所述,尝试通过首先转到 ipynb/jupyter 浏览器会话中的“运行”选项卡来正确终止 ipython 笔记本进程,然后检查控制台上打开的终端并使用 ctrl-c 关闭。 The latter should be avoided if possible.如果可能,应避免后者。

If you run an ipython notebook list and continue to see running ipython servers at different ports, make note of which ports the existing notebooks are being served to.如果您运行ipython notebook list并继续看到在不同端口运行 ipython 服务器,请记下现有 notebook 被提供给哪些端口。 Then shut down your TCP ports:然后关闭你的 TCP 端口:

fuser -k 'port#'/tcp 

I'm not sure if there are other risks involved with doing this.我不确定这样做是否涉及其他风险。 If so, let me know.如果是这样,请告诉我。

Actually, I believe there's a cleaner way than killing the process(es) using kill or task manager.实际上,我相信有一种比使用 kill 或任务管理器杀死进程更清洁的方法。

In the Jupyter Notebook Dashboard (the browser interface you see when you first launch 'jupyter notebook'), browse to the location of notebook files you have closed in the browser, but whose kernels may still be running.在 Jupyter Notebook 仪表板(您首次启动“jupyter notebook”时看到的浏览器界面)中,浏览到您已在浏览器中关闭但其内核可能仍在运行的笔记本文件的位置。

iPython Notebook files appear with a book icon, shown in green if it has a running kernel, or gray if the kernel is not running. iPython Notebook 文件显示一个书本图标,如果内核正在运行则显示为绿色,如果内核未运行则显示为灰色。

Just select the tick box next to the running file, then click on the Shutdown button that appears above it.只需选择正在运行的文件旁边的复选框,然后单击出现在其上方的关机按钮。

This will properly shut down the kernel associated with that specific notebook.这将正确关闭与该特定笔记本关联的内核。

Environment环境


My OS is Ubuntu 16.04 and jupyter is 4.3.0.我的操作系统是 Ubuntu 16.04,而 jupyter 是 4.3.0。

Method方法


First, i logged out jupyter at its homepage on browser(the logout button is at top-right)首先,我在浏览器的主页上注销了 jupyter(注销按钮在右上角)

Second, type in Ctrl + C in your terminal and it shows:其次,在终端中输入Ctrl + C并显示:

[I 15:59:48.407 NotebookApp]interrupted Serving notebooks from local directory: /home/Username 0 active kernels [I 15:59:48.407 NotebookApp]interrupted Serving notebooks from local directory: /home/Username 0 active kernels

The Jupyter Notebook is running at: http://localhost:8888/?token=a572c743dfb73eee28538f9a181bf4d9ad412b19fbb96c82 Jupyter Notebook 运行在: http://localhost:8888/?token=a572c743dfb73eee28538f9a181bf4d9ad412b19fbb96c82

Shutdown this notebook server (y/[n])?关闭此笔记本服务器 (y/[n])?

Last step, type in y within 5 sec, and if it shows:最后一步,在 5 秒内输入y ,如果它显示:

[C 15:59:50.407 NotebookApp] Shutdown confirmed [C 15:59:50.407 NotebookApp] 关机确认
[I 15:59:50.408 NotebookApp] Shutting down kernels [I 15:59:50.408 NotebookApp] 关闭内核

Congrats!恭喜! You close your jupyter successfully.您成功关闭了 jupyter。

Option 1选项1

Open a different console and run打开另一个控制台并运行

jupyter notebook stop [PORT]

The default [PORT] is 8888, so, assuming that Jupyter Notebooks is running on port 8888, just run默认的 [PORT] 是 8888,因此,假设 Jupyter Notebooks 在端口 8888 上运行,只需运行

jupyter notebook stop

If it is on port 9000, then如果它在端口 9000 上,则

jupyter notebook stop 9000

Option 2 ( Source )选项 2来源

  1. Check runtime folder location检查运行时文件夹位置

    jupyter --paths
  2. Remove all files in the runtime folder删除运行时文件夹中的所有文件

    rm -r [RUNTIME FOLDER PATH]/*
  3. Use top to find any Jupyter Notebook running processes left and if so kill their PID.使用top查找剩余的 Jupyter Notebook 运行进程,如果是,则终止它们的 PID。

     top | grep jupyter & kill [PID]

One can boilt it down to可以将其归结

TARGET_PORT=8888
kill -9 $(lsof -n -i4TCP:$TARGET_PORT | cut -f 2 -d " ")

Note : If one wants to launch one's Notebook on a specific IP/Port注意:如果想要在特定 IP/端口上启动笔记本

jupyter notebook --ip=[ADD_IP] --port=[ADD_PORT] --allow-root &

The best way now is to use the "Quit" button that is just to the left of the "Logout" button.现在最好的方法是使用“退出”按钮左侧的“退出”按钮。 I have to admit that I do not understand the utility of the Logout button.我不得不承认,我不明白注销按钮的效用。 However, I am glad that they have added the exceedingly useful Quit button.但是,我很高兴他们添加了非常有用的退出按钮。

If you are on mac and using Jupyter lab you can do the following in terminal如果您在 Mac 上并使用 Jupyter 实验室,您可以在终端中执行以下操作

jupyter list

Above will give you all the running servers.以上将为您提供所有正在运行的服务器。 Once you get to know the post just use jupyter lab stop.了解帖子后,只需使用 jupyter lab stop。 For example if the server was running on 8888 just execute例如,如果服务器在 8888 上运行,只需执行

jupyter lab stop 8888

在浏览器会话中,您还可以转到Kernel ,然后单击Restart and Clear Output

I am copy pasting from the Jupyter/IPython Notebook Quick Start Guide Documentation, released on Feb 13, 2018. http://jupyter-notebook-beginner-guide.readthedocs.io/en/latest/execute.html我复制从Jupyter / IPython的笔记本电脑快速入门指南文档粘贴,发布了2月13日,2018年http://jupyter-notebook-beginner-guide.readthedocs.io/en/latest/execute.html

1.3.3 Close a notebook: kernel shut down When a notebook is opened, its “computational engine” (called the kernel) is automatically started. 1.3.3 关闭笔记本:内核关闭 当笔记本打开时,它的“计算引擎”(称为内核)会自动启动。 Closing the notebook browser tab, will not shut down the kernel, instead the kernel will keep running until is explicitly shut down.关闭笔记本浏览器选项卡不会关闭内核,而是内核将继续运行,直到被明确关闭。 To shut down a kernel, go to the associated notebook and click on menu File -> Close and Halt.要关闭内核,请转到关联的笔记本并单击菜单 File -> Close and Halt。 Alternatively, the Notebook Dashboard has a tab named Running that shows all the running notebooks (ie kernels) and allows shutting them down (by clicking on a Shutdown button).或者,Notebook Dashboard 有一个名为 Running 的选项卡,显示所有正在运行的 notebook(即内核)并允许关闭它们(通过单击 Shutdown 按钮)。

Summary: First close and halt the notebooks running.总结:首先关闭并停止正在运行的笔记本。

1.3.2 Shut down the Jupyter Notebook App Closing the browser (or the tab) will not close the Jupyter Notebook App. 1.3.2 关闭 Jupyter Notebook App 关闭浏览器(或选项卡)不会关闭 Jupyter Notebook App。 To completely shut it down you need to close the associated terminal.要完全关闭它,您需要关闭相关的终端。 In more detail, the Jupyter Notebook App is a server that appears in your browser at a default address ( http://localhost:8888 ).更详细地说,Jupyter Notebook 应用程序是一个显示在浏览器中默认地址 ( http://localhost:8888 ) 的服务器。 Closing the browser will not shut down the server.关闭浏览器不会关闭服务器。 You can reopen the previous address and the Jupyter Notebook App will be redisplayed.可以重新打开之前的地址,Jupyter Notebook App 会重新显示。 You can run many copies of the Jupyter Notebook App and they will show up at a similar address (only the number after “:”, which is the port, will increment for each new copy).您可以运行 Jupyter Notebook 应用程序的多个副本,它们将显示在类似的地址处(只有“:”之后的数字,即端口,会随着每个新副本的增加而增加)。 Since with a single Jupyter Notebook App you can already open many notebooks, we do not recommend running multiple copies of Jupyter Notebook App.由于使用单个 Jupyter Notebook 应用程序您已经可以打开多个笔记本,因此我们不建议运行多个 Jupyter Notebook 应用程序副本。

Summary: Second, quit the terminal from which you fired Jupyter.总结:第二,退出你启动 Jupyter 的终端。

步骤 1 - 在 shell 上执行 control+z (control+c) 步骤 2 _ 关闭网络浏览器

How to close IPython Notebook properly?如何正确关闭IPython Notebook?

Currently, I just close the browser tabs and then use Ctrl+C in the terminal.目前,我只是关闭浏览器选项卡,然后在终端中使用Ctrl+C
Unfortunately, neither exit() nor ticking Kill kernel upon exit does help (they do kill the kernel they but don't exit the iPython).不幸的是, exit()Kill kernel upon exit都勾选Kill kernel upon exit都无济于事(它们确实杀死了他们的内核,但不退出iPython)。

To kill all jupyter-notebook and jupyter-lab , open terminal and just kill all jupyter that running in background by giving要杀死所有jupyter-notebookjupyter-lab ,打开终端并通过提供杀死所有在后台运行的jupyter

pkill jupyter

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM