Open In App

How to interrupt the kernel in Jupyter notebook

Last Updated : 27 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will cover How to interrupt the kernel in Jupyter Notebook we will discuss various methods to do the same. First, we look at what is Jupyter Notebook followed by various methods to interrupt the kernel in Jupyter Notebook.

Jupyter Notebook

The Jupyter Notebook is the original web application for creating and sharing computational documents that contain live code, equations, visualizations, and narrative text. It offers a simple, streamlined, document-centric experience. Jupyter has support for over 40 different programming languages and Python is one of them.

Kernel

In the context of Jupyter Notebook, the kernel is an engine that executes the code within the notebook, it interprets and runs the code in the notebook and manages the execution environment. There are different kernels available in the Jupyter Notebook like the Python3 kernel to execute the code of Python. Sometimes kernel gets stuck in an infinite loop or the code cell execution doesn’t stop and we have to forcefully interrupt the kernel in this article we will cover various methods to interrupt the kernel.

Interrupting the kernel in Jupyter notebook

There are 3 ways to interrupt the kernel in Jupyter Notebook which are as follows:

  • Using Keyboard shortcut
  • Using Menu options
  • Using Toolbar

1. Keyboard Shortcut

To delete a cell using a keyboard shortcut, follow these steps:

  • Press “i i” i.e. press “i” twice to interrupt

You can also edit keyboard shortcuts by selecting Menu option Help -> Edit Keyboard Shortcuts then select interrupt kernel and add new shortcut key, refer to below screenshot,

Screenshot-from-2023-09-13-12-24-59

2. Using Menu Options

Follow the below steps to interrupt the kernel using menu options,

  • Navigate to Menu Options Kernel -> Interrupt to interrupt the kernel

Refer the below screenshots,

Screenshot-from-2023-10-10-18-57-04

3. Using Toolbar

To interrupt the kernel from the toolbar click on “Square” icon in the toolbar it will interrupt the kernel,

Screenshot-from-2023-10-10-18-55-07

Why Interrupting the Kernel is Necessary

To understand why interrupting the kernel is nescessay let’s consider an example in which a code block is running an infinite loop or a recursion without base case in both the scenerios the kernel will executes the code block indefinite time until any memory fault or any other error arises. Hence we will have to interrupt the kernel which will helps in debugging the code and optimize the code to make the program more time efficient, interrupting the kernel will helps to halt the code block that may consume excessive CPU time or memory resources, preventing system slowdown.

What Happens When You Interrupt the Kernel

When the kernel is interrupted first thing that happens is the code execution got stopped and any code cell running in the notebook got forcefully halted by the system and any output generated by that cell upto that point is displayed. When the kernel is interrupted the kernel is still active and user can continue its work in notebook and also the state of the kernel like variable, libararies that are imported etc are preserved, user can continue work in the notebook without loosing its state.

Handling Interrupted Kernels

When the kernel is interrupted there are various ways to handle them like user can continue working on the notebook since the state of the kernel is preseved, or user can choose to shutdown the kernel completely by going to menu options Kernel -> Shutdown or user can restart the kernel and re-run all the cells which clears the state of the kernel and executes all the cells sequentially to get the updated outputs by going to menu options Kernel -> Restart & Run all.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads