Skip to main content

Interrupting the Kernel

When executing a long-running cell using the %%ag magic command, you might need to interrupt the operation due to complex computations, large datasets, or unexpected infinite loops. Conveniently, this can be done using keyboard shortcuts in Jupyter Notebook, VSCode, and Google Colab.

Keyboard Shortcuts

Before using the keyboard shortcuts, make sure you are not in "edit mode" for any cell. You can exit "edit mode" by pressing the Esc key. The cell border should change from green to blue in Jupyter Notebook and Google Colab, indicating that you are in "command mode". For different environments, the keyboard shortcuts to interrupt a running cell are:

  • Jupyter Notebook: I,I (press I key twice)
  • VSCode: Use the stop icon in the Python Interactive window.
  • Google Colab: Clrt + M I

This will stop the current execution and give a message indicating a successful interruption.

%%ag
for _ in range(int(1e10)):
pass

On successful interrupt: "Kernel interrupted successfully"

Precautions When Interrupting the Kernel

If you have initiated a long-running process and want to stop it mid-way, interrupting the kernel is your go-to solution. However, be cautious while doing so, as it abruptly stops the process, which might lead to unpredictable results if the process was modifying a shared state.