How to stop the running cell if interupt kernel does not work in Jupyter Notebook
Jupyter NotebookJupyter Notebook Problem Overview
I have been using Jupyter Notebook for a while. Often when I try to stop a cell execution, interrupting the kernel does not work. In this case, what else can I do, other than just closing the notebook and relaunching it again? I guess this might be a common situation for many people.
Jupyter Notebook Solutions
Solution 1 - Jupyter Notebook
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
Solution 2 - Jupyter Notebook
If you're ok with losing all currently defined variables, then going to Kernel > Restart will stop execution without closing the notebook.
Solution 3 - Jupyter Notebook
Recently I also faced a similar issue.
Found out that there is an issue in Python https://github.com/ipython/ipython/issues/3400 and it was there for 6 some years and it has been resolved as of 1st March 2020.
Solution 4 - Jupyter Notebook
One thing that might work is hitting interrupt a bunch of times. It's possible that a library you are using catches the interrupt signal and only stops after receiving the signal multiple times.
For example, when using sklearn's cross_val_score() I found that I have to interrupt once for each cross validation fold.
Solution 5 - Jupyter Notebook
This worked for me:
- Put the laptop to sleep (one of the power options)
- Wait 10 s
- Wake up computer (with power button)
Kernel then says reconnecting and its either interrupted or you can press interrupt.
Probably isn't fool proof but worth a try so you don't waste previous computation time. (I had Windows 10 running a Jupyter Notebook that wouldn't stop running a piece of Selenium code)
Solution 6 - Jupyter Notebook
There are a few options here:
-
Change the folder name of data: Works if the cell is running already and pulling data from a particular folder. For example I had a for loop that when interrupted just moved to the next item in list it was processing.
-
Change the code in the cell to generate an error: Works if the cell has not been run yet but is just in queue.
-
Restart Kernel: If all else fails
Solution 7 - Jupyter Notebook
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
or ps aux | grep ipython
), say 3873. Then, write a script that saves the state for example to a pickle in a file inject.py
, say, it is a Pandas dataframe df
in the global scope:
df.to_pickle("rescued_df.pkl")
Finally, 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
Solution 8 - Jupyter Notebook
You can force the termination by deleting the cell. I copy the code, delete the cell, create a new cell, paste, and execute again. Works like a charm.
Solution 9 - Jupyter Notebook
I suggest to restart the kernel. It will be ready to use then .