Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content
No results
  • About AskPython
  • Blog
  • Contact Me
  • Free Online Developer Tools Hub
  • Home
  • Meet the Team
  • Privacy Policy
  • Python
  • Python Course for Beginners Online FREE
  • Python Tutorial – Mega Index
  • Terms and Conditions

🚀 Supercharge your YouTube channel's growth with AI.

Try YTGrowAI Free
  • Home
  • Blog
  • About AskPython
  • Python Course
  • Start Here

Python Signal Module – What are Signals and How to Create Them?

  • Vijaykrishna RamVijaykrishna Ram
  • July 24, 2020
  • Python Modules
Python Signal Module – What are Signals and How to Create Them?

In this article, we’ll take a look at using the Python signal module.

This module is very useful when we want to handle certain signals using Python.

First, let’s look at what a signal means.


What is a Signal?

A Signal is a means through which a program can receive information from the Operating System. When the operating system receives certain events, it can pass that to programs in the form of signals.

For example, when we press the keystrokes Ctrl + C on our keyboard, the Operating System will generate a signal and pass this on to programs. For this particular combination, the signal SIGINT is generated and passed to the programs.

For all common operating systems, there is a standard pattern for assigning these signals, which are generally short for integers.

In Python, these signals are defined in the signal module.

import signal

To look at all the valid signals in your system (depends on the OS), you can use signal.valid_signals()

import signal

valid_signals = signal.valid_signals()

print(valid_signals)

Output

{<Signals.SIGHUP: 1>, <Signals.SIGINT: 2>, <Signals.SIGQUIT: 3>, <Signals.SIGILL: 4>, <Signals.SIGTRAP: 5>, <Signals.SIGABRT: 6>, <Signals.SIGBUS: 7>, <Signals.SIGFPE: 8>, <Signals.SIGKILL: 9>, <Signals.SIGUSR1: 10>, <Signals.SIGSEGV: 11>, <Signals.SIGUSR2: 12>, <Signals.SIGPIPE: 13>, <Signals.SIGALRM: 14>, <Signals.SIGTERM: 15>, 16, <Signals.SIGCHLD: 17>, <Signals.SIGCONT: 18>, <Signals.SIGSTOP: 19>, <Signals.SIGTSTP: 20>, <Signals.SIGTTIN: 21>, <Signals.SIGTTOU: 22>, <Signals.SIGURG: 23>, <Signals.SIGXCPU: 24>, <Signals.SIGXFSZ: 25>, <Signals.SIGVTALRM: 26>, <Signals.SIGPROF: 27>, <Signals.SIGWINCH: 28>, <Signals.SIGIO: 29>, <Signals.SIGPWR: 30>, <Signals.SIGSYS: 31>, <Signals.SIGRTMIN: 34>, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, <Signals.SIGRTMAX: 64>}

Now, for all signals, there are some default actions which the OS will assign to every program.

If we want to have some other behavior instead, we can use signal handlers!


What is a Python Signal Handler?

A Signal Handler is a user defined function, where Python signals can be handled.

If we take the signal SIGINT (Interrupt Signal), the default behavior would be to stop the current running program.

We can, however, assign a signal handler to detect this signal and do our custom processing instead!

Let’s take a look at how we can do this.

import signal  
import time  

# Our signal handler
def signal_handler(signum, frame):  
    print("Signal Number:", signum, " Frame: ", frame)  

def exit_handler(signum, frame):
    print('Exiting....')
    exit(0)

# Register our signal handler with `SIGINT`(CTRL + C)
signal.signal(signal.SIGINT, signal_handler)

# Register the exit handler with `SIGTSTP` (Ctrl + Z)
signal.signal(signal.SIGTSTP, exit_handler)

# While Loop
while 1:  
    print("Press Ctrl + C") 
    time.sleep(3) 

Here, after we run our program, when we press Ctrl + C, the program will go to the signal_handler() function, since we have registered the handler with SIGINT (Ctrl + C).

We also have another handler exit_handler() which exits the program if we press Ctrl + Z, which sends a SIGTSTP signal.

Let’s look at the output

Output

Press Ctrl + C
^CSignal Number: 2  Frame:  <frame at 0x7fe62f033640, file 'python_signals.py', line 22, code <module>>
^ZExiting....

Here, I pressed Ctrl + C to go to the signal_handler() function, and then pressed Ctrl + Z to exit the program. Notice that there is a stack frame object (frame) as well, to track the runtime stack of the main program.


Using Alarm Signals

We can use the SIGALARM signal to send alarm signals to our program. Let’s write a simple signal handler that handles this Python signal.

import signal  
import time  

def alarm_handler(signum, frame):  
    print('Alarm at:', time.ctime())  

# Register the alarm signal with our handler
signal.signal(signal.SIGALRM, alarm_handler)

signal.alarm(3)  # Set the alarm after 3 seconds  

print('Current time:', time.ctime())  

time.sleep(6)  # Make a sufficient delay for the alarm to happen 

In the last line, we sleep for sufficient time (6 seconds) to allow the alarm signal to pass to our program. Otherwise, since the program would have terminated, the signal won’t be received!

Output

Current time: Thu Jul 23 00:41:40 2020
Alarm at: Thu Jul 23 00:41:43 2020

Conclusion

In this article, we learned about how we can set up signal handlers to handle various signals, using the signal module. Visit the link here to learn more about Python modules

References

  • Python signal module Documentation
  • JournalDev article on Python Signals

Vijaykrishna Ram
Vijaykrishna Ram
Articles: 99
Python HowTo – Sending Emails using Python
Previous Post Python HowTo - Sending Emails using Python
Next Post Permutations and Combinations using Python
Permutations and Combinations using Python

Related Posts

np.linalg.eig: Eigenvalues and Eigenvectors

np.linalg.eig: Eigenvalues and Eigenvectors

  • November 30, 2026
np.sin() in NumPy: Radians, Arrays, and Examples

How to Use np.sin() in NumPy

  • November 30, 2026
cv2.imread: Load Images with OpenCV

cv2.imread: Load Images with OpenCV

  • November 30, 2026

Partner Websites

  • GoLangDocs
  • LinuxForDevices
  • CodeForGeek

Popular Categories

  • Python Programming
  • Python Modules
  • OOPS in Python
  • Python Programming Examples
  • Strings in Python

Hall of Fame

  • Python Programming
  • OOPS in Python
  • Creating Python Modules
  • Args and Kwargs in Python
  • Python Dictionary (Dict) Tutorial
  • Python List – 15 Things You MUST Know
  • Python Set – Things You MUST Know
  • Python String Functions: Methods by Task

Important Links

  • Home
  • Python Course
  • Start Here
  • Blog
  • About AskPython
Latest posts
  • np.linalg.eig: Eigenvalues and Eigenvectors
  • What Does %s Mean in a Python Format String?
  • How to Use np.sin() in NumPy
  • cv2.imread: Load Images with OpenCV
  • pd.read_json: Read JSON Into a DataFrame

Copyright © 2026 - AskPython. All rights reserved.