Learn How to Capture and Handle OS Signals Like SIGINT (CTRL-C) in Python for Beginners

by xanthium-enterprises in Circuits > Raspberry Pi

1720 Views, 2 Favorites, 0 Comments

Learn How to Capture and Handle OS Signals Like SIGINT (CTRL-C) in Python for Beginners

Capturing and Handling Operating System signals like SIGINT (CTRL-C) ,SIGBREAK in Python 3 Tutorial

Here we will learn how to capture and handle operating system signals like SIGINT and SIGBREAK on Linux and Windows OS's to control the flow of your python script during execution.

We will use the signal module from Python 3 to capture and handle the OS signals.

Supplies

  • Python 3 Interpreter
  • Text editor

Contents

Source Codes

github.jpg


Instructable code shown here may be partial or pseudo code just enough to highlight relevant techniques or methods. 


Please use the full source codes from our Github Repo to avoid errors

 


What Are Signals

ctrl-c-signal-handling-python3.jpg

Signal is a way in which a program or process (here our python script) can receive information from the Operating System that certain events like a Keyboard Press (CTRL + C ) or a Fault have happened. 


Signals are assigned a Integer Value by the OS.


When we press CTRL + C on our Keyboard the Operating System (Both Windows and Linux) will generate a SIGINT signal which is send to the program which is currently active.


Not all signals are available on all systems. Signals differ between operating systems(Linux/Windows)


Checking for Available Signals in Your OS.

Since not all signals are available in every OS, It is good to know which ones are available on your system.

Here we will be using Python 3 (Python 3.9.x) and to access the signals we have to import the signal module

import signal    # Import signal module 

# available signals on our System
valid_signals = signal.valid_signals() # requires python 3.9.0
# returns a SET

print('Number of Available Signals ->', len(valid_signals) , '\n')
for i in valid_signals:
print(i)

The above partial code will print available signals on a particular OS. Download the full codes from Github

Here  

valid_signals = signal.valid_signals()

returns a set of available OS specific signals which are then printed out.

Available OS Signals on Windows 10 


Programming Signals on Python 3

To use the Signal received by our script we have to do two things

  1. Write a Signal Handler which will do the custom processing ,when Signal is received
  2. Register the Signal with the Signal Handler function

 

import signal

def your_custom_signal_handler(signal_number,frame):
code to do something
code to do something
code to do something

signal.signal(signal.SIGNALNAME,your_custom_signal_handler)


Here we will be using the SIGINT signal to control the execution of our Python 3 script.

SIGINT Signal is common to both Windows and Linux systems and helps us to run our code on both Windows and Linux Systems.

Please use the code from Github

import signal # Import signal module

def SignalHandler_SIGINT(SignalNumber,Frame):
print('SignalHandler of signal.SIGINT')

#register the signal with Signal handler
signal.signal(signal.SIGINT,SignalHandler_SIGINT)

while 1:
print("Press Ctrl + C ")
time.sleep(1)

 

Here 

def SignalHandler_SIGINT(SignalNumber,Frame):

is the Signal Handler ,So when the Signal SIGINT is generated this function is called and the print() statement under is called.

 

The statement

signal.signal(signal.SIGINT,SignalHandler_SIGINT)

registers the signal handler function SignalHandler_SIGINT(SignalNumber,Frame): with the signal SIGINT  signal.SIGINT

Please note that the code should be run under the command line.

If you use an IDE like Thonny or IDLE, the IDE tend to interfere with the reception of the SIGINT signal

Safely Exiting From an Infinite Loop in Python Using CTRL + C (SIGINT)

In this example we will learn how to safely exit from an infinite loop in Python 3 using signals.

In some application like Serial Port Data Acquisition System Software written in Python we have to query a sensor or Microcontroller like Arduino or Raspberry Pi board continuously in a infinite loop.

In those applications we can use the SIGINT signal (CTRL +C) to interrupt the infinite loop and close the application safely without a resource leak.

Check this article Exiting From An Infinite Loop In Python Using CTRL + C (SIGINT) for more info

Getting out of the infinite loop in Python on Windows

Getting out of the infinite loop in Python on Linux using SIGINT

This technique is used to build a Python based serial port data acquisition system here