The signal module performs a specific action on receiving signals. Even it has the ability to capture the interruption performed by the user through the keyboard by use of SIGINT. This article will discuss SIGINT only, how to capture it, and what to do after it has been captured.
Modules Required:
Signal: The technique through which a program can receive information from the operating system is known as a signal. Further, the signals are passed to the program when the operating system has received certain events. The signal module can be downloaded by running the following command in the terminal:
pip install signals
Sys: The Python sys module that provides various functions and variables to manipulate various parts of the Python run environment is called the sys module. You can download the sys module through the following command:
pip install os-sys
Time: The time module in Python which allows the user to work with time and capture details related to time is known as the Python module. The time module usually comes preinstalled with Python, so there is no need to install it, but in case, it hasn’t come preinstalled with your Python, then you can use the following command:
pip install python-time
Stepwise Implementation:
Step 1: First of all, we need to import some libraries of Python. These libraries include signal, sys, and sleep.
Python3
import signal import sys from time import sleep |
Step 2: Now, we create a function to be called with any two arguments when a keyboard interruption is made. Here, we have taken the arguments as sig and frame.
Python3
def signal_handler(sig, frame): |
Step 3: In this step, we define custom handlers that need to be executed on receiving a signal using the signal.signal() function. Also, we define signal.SIGINT, which will perform interruption through the keyboard either by pressing Ctrl+ C or Ctrl+F2.
Python3
signal.signal(signal.SIGINT, signal_handler) |
Step 4: Later on, display some lines as a message to make the user know what to do for keyboard interruption.
Python3
print ( '#Lines to display' ) |
Step 5: Finally, make Python sleep for some time.
Python3
sleep(Time duration in seconds) |
Note: The catch in the program is that if you running this program on Windows, then you can stop the program, i.e., capture SIGINT by pressing Ctrl and F2 simultaneously while if you are running this program on Linux, then you can stop the program by pressing Ctrl and C simultaneously.
How to capture SIGINT in Python
In this program, we capture keyboard exceptions using a try-catch statement. In the try block, we had run an increasing loop of numbers, while in the catch block, we captured the keyboard interruption.
Python
# Python program to capture # SIGINT in Python # Import the libraries time, sys import sys from time import sleep # Declare the variable with value 1 x = 1 # Construct an infinite while loop while True : # Make a try-catch statement try : # Print the loop of numbers print (x) # Make Python sleep for some time # between print of each number sleep(. 3 ) # Increase the loop by 1 x + = 1 # Catch the exception of keyboard interruption except KeyboardInterrupt: # Print the statement for exception print ( "Loop is stopped" ) # Close the program sys.exit() |
Output: