How to stop the running cell if interupt kernel does not work in jupyter notebook

*爱你&永不变心* 提交于 2020-01-12 11:47:10

问题


I'm using jupyter notebook for a while. Often when I tried to stop a cell execution, interrupting kernel did not work. In this case, what else can I do, other than just closing the notebook and relaunch again? I guess this might be a common situation for many people. thanks a lot.


回答1:


If you're ok with losing all currently defined variables, then going to Kernel > Restart will stop execution without closing the notebook.




回答2:


Currently this is an issue in the github jupyter repository as well, https://github.com/ipython/ipython/issues/3400 there seems to be no exact solution for that except killing the kernel




回答3:


Put in comment (highlight and press Ctrl-/) the instruction(s) responsible for running -- or, faster, comment the whole cell -- and re-run the cell (Ctrl-Enter). This will stop running and of course the output. You can then un-comment the affected part. This is much less painful than killing and restarting the kernel.

(Note that just clearing the output with [Esc]+'O' won't stop it.)




回答4:


If the iPython kernel did not die, you might be able to inject Python code into it that saves important data using pyrasite. You need to install and run pyrasite as root, i.e. with sudo python -m pip install pyrasite or python3 as needed. Then you need to figure out the process id (PID) of the iPython kernel (e.g. via htop), say 3873. Finally, write a script that saves the state for example to a pickle in a file inject.py and inject it into the process as follows:

sudo pyrasite 3873 inject.py

You may need to enable dtrace first like so:

echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope


来源:https://stackoverflow.com/questions/42750753/how-to-stop-the-running-cell-if-interupt-kernel-does-not-work-in-jupyter-noteboo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!