80

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.

jimh
  • 1,651
  • 2
  • 15
  • 28
zesla
  • 11,155
  • 16
  • 82
  • 147
  • 2
    Unfortunately as of 2022 the answer is still that there is no good solution. This is an issue going back over 10 years. The fact that it is still unresolved suggests it is too hard or low priority to expect a fix soon. – eric Aug 04 '22 at 15:51

11 Answers11

23

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

Harish Rajula
  • 699
  • 6
  • 11
9

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

hamdog
  • 991
  • 2
  • 10
  • 24
5

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)

F. Bishton
  • 53
  • 1
  • 3
5

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

EddyWD
  • 197
  • 2
  • 15
3

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.

Burrito
  • 1,475
  • 19
  • 27
2

If you know in advance that you might want to stop without losing all your variables, the following solution might be useful:

In cells that take a while because of long loops, you may implement something like this in the loop:

if os.path.exists(os.path.join(os.getcwd(),'stop_true.txt')):
    break

Then if you want to stop just create the file 'stop_true.txt'. And the loop stops before the next round.

Usually, the file is called 'stop_false.txt' until I rename it to stop the loop.

Additionally, the results of each loop are stored in a dictionary separately. Therefore I'm able to keep all results until the break happened and can restart the loop from this point onwards.

Thomas R
  • 1,067
  • 11
  • 17
0

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
Robin Dinse
  • 1,491
  • 14
  • 20
0

For me, setting up a time limit worked: https://github.com/scipopt/PySCIPOpt/issues/197. Specifically, I added "model.setRealParam("limits/time", 60)" piece of code and it automatically stops calculation after 60 seconds. You can set up any time instead of 60. But this is for pyscipopt package (solving optimization model). I am not sure how to set up the time limit for your specific problem.

Lidiia S
  • 11
  • 1
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 26 '22 at 17:30
0

Try this:

  1. Close the browser tab in which Jupyter is running
  2. Run jupyter server list
  3. Kill each running server with jupyter server stop <PORT>
robertspierre
  • 3,218
  • 2
  • 31
  • 46
-3

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.

  • 1
    what about the cell containing async operations? – nefo_x May 15 '21 at 13:21
  • 1
    @nefo_x you are going to need a much more sophisticated answer for this question. None of the answers here would save that, and to be honest I'm not aware of any Jupyter feature to stop one thread. – Lee Kezar May 16 '21 at 15:41
-7

I suggest to restart the kernel (Kernel -> Restart Kernel) as suggested by @hamdog.

It will be ready to use after that. However, it will certainly delete all variables stored in memory.

blackraven
  • 5,284
  • 7
  • 19
  • 45
Pula94
  • 13
  • 1