Adventures in Machine Learning

Handling Operating System Signals in Python: A Comprehensive Guide

Introduction to Python Signal Module

In the world of programming, a signal is a software interrupt that is generated by an operating system. Python, being an operating system, has its own signal module that is responsible for handling system signals.

This module allows programmers to register signal handlers- user-defined functions that take some action when an OS signal is generated. In this article, we will explore the Python signal module and learn how to handle signals in Python.

Valid Signals in System

Before we dive into signal handling, it is essential to understand the different types of signals an operating system can generate. On the Unix/Linux system, the valid set of signals is represented by integers ranging from 1 to 31.

The signal module in Python provides symbolic constants to represent these signals. For example, signal.SIGINT represents the signal that is generated when the user presses the interrupt key (usually Ctrl + C) on their keyboard.

Some other commonly used signals are signal.SIGTSTP (generated when the user presses Ctrl + Z) and signal.SIGALRM (generated by the alarm clock). The complete list of valid signals can be found in the signal module documentation.

Python Signal Handlers

A signal handler is a user-defined function that is executed in response to a signal being generated. Essentially, when a signal is generated, the operating system interrupts the execution of the current program, raises the specific signal, and executes the associated signal handler.

The signal module in Python provides the signal.signal() function that is used to register a signal handler. The handler function should be designed to take two arguments: signal_number and frame, standing for the signal number raised and the Python frame object of the stack at the time the signal was received, respectively.

Registering a Signal Handler

Now that we have an overview of the signal module and the role of a signal handler, let’s take a deep dive into how to register a signal handler in Python. First, we need to import the signal module to gain access to its functions and constants:

import signal

Then, we can register a signal handler using the signal.signal() function, passing in the signal number and the handler function as arguments:

def handler(signal_number, frame):

print(f”Signal {signal_number} received”)


signal.signal(signal.SIGINT, handler)

In the above code snippet, we define a basic signal handler that prints which signal was received and exits the program with a status code of 0. We then register this handler function to handle the SIGINT signal (generated by pressing Ctrl + C) using the signal.signal() function.

Output with Signal Handler

When a signal is generated, the operating system will interrupt the execution of the program and call the associated signal handler function. Let’s run the above code and see what happens when we press Ctrl + C while the program is running:

$ python

# Press Ctrl + C

Signal 2 received

As expected, pressing Ctrl + C generates the SIGINT signal, which causes the signal handler function to print that the signal was received and exit the program gracefully.

Using Alarm Signals

Apart from the standard signals like SIGINT and SIGTSTP, the signal module in Python provides the ability to set timed, single-use alarms with the SIGALRM signal. This feature is handy when we want to trigger a specific function or code after a specific amount of time.

The signal.alarm() function is used to set up a single-use alarm signal. This function takes an integer argument that represents the number of seconds before the signal is generated.

Once the specified time has elapsed, an alarm signal is generated and the associated signal handler function is executed. For example, let’s create a signal handler function that prints “Wake up!” when the SIGALRM signal is received, and set an alarm for 5 seconds:

import signal

import time

def handler(signal_number, frame):

print(“Wake up!”)

signal.signal(signal.SIGALRM, handler)


while True:


In the above code snippet, the signal handler function prints “Wake up!” when the SIGALRM signal is received. We register this function to handle the SIGALRM signal using the signal.signal() function and set an alarm using signal.alarm(5), which generates the alarm signal for after 5 seconds.

The while loop is used to keep the program running indefinitely. We use time.sleep(1) to pause the execution for one second in each iteration so that the program doesn’t consume too much CPU by running an infinite loop.


The signal module in Python provides a powerful mechanism for handling operating system signals. Using the signal.signal() function, we can register a signal handler function that takes some action when a signal is generated, like printing a message and terminating the program.

We can use standard signals like SIGINT and SIGALRM to handle interrupt signals and delayed, single-use alarms, respectively. With a proper understanding of signal handling, programmers can write better, more robust Python programs.

Setting up Signal Handlers with Signal Module

Signal handlers are a crucial part of programming as they provide us with a way to respond to external events that are outside of our program’s control. The signal module in Python provides a simple yet powerful interface to handle these external events.

In this section, we will look at how to set up signal handlers in Python using the signal module.

Registering a Signal Handler

Before we dive into setting up signal handlers, let us review the signal.signal() function, which is used to register a signal handler. The signal.signal() function takes two arguments: the signal number and the signal handler function.

The signal number specifies which signal we want to listen for, while the signal handler function is responsible for the code that is executed when the signal is received.

The process for registering a signal handler is as follows:


Import the signal module. 2.

Define your signal handler function. 3.

Register the signal handler function with the signal.signal() function, passing the appropriate signal number. For example, the following code registers a signal handler for the SIGINT signal:


import signal

def ctrl_c_handler(signal, frame):

print(“CTRL+C detected. Exiting…”)


signal.signal(signal.SIGINT, ctrl_c_handler)


Once registered, the signal handler will execute whenever the corresponding signal is raised.

In this case, whenever a SIGINT signal is generated (for example, when the user presses CTRL+C in the terminal), the ctrl_c_handler() function will be called.

Valid Signals

There are a number of signals that can be triggered on a Unix-based operating system. The signal module in Python provides constants to represent these signals, making it easier to work with them in code.

These constants typically begin with the prefix SIG, followed by the abbreviated name of the signal. Examples of commonly-used signals include:

– SIGABRT: Generated when the program calls the abort() function.

– SIGFPE: Generated when a floating-point exception occurs. – SIGILL: Generated when the program attempts an illegal instruction.

– SIGINT: Generated when the user hits CTRL+C in the terminal. – SIGSEGV: Generated when the program attempts to access memory it doesn’t have permission to access.

– SIGTERM: Generated when the program receives a termination signal (typically issued by a shutdown command). A complete list of valid signals and their symbolic constants can be found in the Python documentation.

Handling Alarms

Another useful feature of the signal module is the ability to set an alarm signal. Using the signal.alarm() function, we can schedule the generation of a SIGALRM signal after a specified number of seconds.

This feature is often used to schedule events or tasks to occur at a given time. Here’s an example of how this might be used:


import signal

def handle_alarm(signal_number, frame):

print(‘ALARM signal received!’)

signal.signal(signal.SIGALRM, handle_alarm)

signal.alarm(5) # Schedule an ALARM signal to be raised in 5 seconds. input(‘Press Enter to stop…’) # Wait for user input before exiting.


In this example, we define a signal handler function called handle_alarm() which simply prints a message to the console. We then register this function to handle the SIGALRM signal using signal.signal(), and schedule an ALARM signal to be generated in 5 seconds using signal.alarm(5).

The program then waits for the user to enter some input before it exits.

Additional Resources

Now that we’ve covered the basics of setting up signal handlers in Python with the signal module, you may be interested in exploring the topic further. The Python documentation provides a more comprehensive reference to the signal module, which provides several additional features that we haven’t covered in this article.

You may also want to investigate some third-party Python modules for working with signals. For example, the PySignal library provides a more high-level and object-oriented interface to signal handling, which can help to simplify more complex signal handling scenarios.

Similarly, the Gevent library provides a cooperative multitasking framework, which includes support for asynchronous I/O and signal handling. In conclusion, the signal module provides a powerful yet straightforward mechanism for handling signals in Python.

By registering signal handlers with the signal.signal() function, we can respond to external events like user input and system-generated signals. This, in turn, allows us to write more robust and flexible programs.

In conclusion, the Python signal module provides programmers with an effective way to handle operating system-generated signals. Registering signal handlers with the signal module’s signal.signal() function gives us the ability to respond to events such as user input and timed alarms.

Additionally, the signal module provides symbolic constants that simplify working with valid signals. The article also highlights third-party libraries that offer additional support for signal handling.

Using signal handlers, we can write more robust programs that gracefully handle external events. Going forward, programmers should take advantage of this tool to write better and more flexible programs.

Popular Posts