Welcome to python signal processing tutorial. This is going to be a very basic lesson on how to handle signals with python. A computer receives and transmits various commands with the help of signals. An operating system has got at least one signal listed for each type of command it is programmed to receive or generate. And now, we will look at the basic signal processing in python 3.
What is Signal
A signal is like a notification of an event. When an event occurs in the system, a signal is generated to notify other programs about the event. For example, if you press Ctrl + c
, a signal called SIGINT
is generated and any program can actually be aware of that incident by simply reading that signal. Signals are identified by integers.
Python signal processing
Python signal module is required for almost all the basic signal handling operations in python.
To understand how python signal processing works, we need to know about ‘signal handler’. Signal handler is a task or program, which is executed when a particular signal is detected. A handler takes two arguments, namely, the signal number and a frame.
You may just print a line in the handler or initiate some other task in response to the signal. signal.signal()
function assigns handlers to a signal. Now without further discussion, let’s proceed to an example.
Python Signal Example
The following example shows how to program the system to respond to a SIGINT signal.
import signal
import time
def handler(a, b): # define the handler
print("Signal Number:", a, " Frame: ", b)
signal.signal(signal.SIGINT, handler) # assign the handler to the signal SIGINT
while 1:
print("Press ctrl + c") # wait for SIGINT
time.sleep(10)
Lines 1 and 2 imports the signal and time module to be used in the program.
Signal handler is defined in lines 5 to 6. It prints the integer value of the signal and the frame it receives along with the signal.
Line 8 uses signal.signal() function to assign the handler to the signal SIGINT. This means every time the CPU picks up a ‘ctrl + c’ signal, the function handler is called.
Line 10 to 12 is written to keep the program running indefinitely.
To execute this program, save the code as python_signal.py
and open command window at the same folder. Then run the command python python_signal.py
in the command window. The program should be running by then. Now press ctrl + c
to obtain the following result. You may terminate the program by simply closing the command window or pressing ctrl + z
.
Below image shows the output produced by above python signal processing example.
Python Signal Alarm
Let’s see another example to demonstrate the use of SIGALARM
signal.
import signal
import time
def alarm_handler(signum, stack):
print('Alarm at:', time.ctime())
signal.signal(signal.SIGALRM, alarm_handler) # assign alarm_handler to SIGALARM
signal.alarm(4) # set alarm after 4 seconds
print('Current time:', time.ctime())
time.sleep(6) # make sufficient delay for the alarm to happen
Above python alarm signal program produces following output.
In this example, it is notable that a delay is made at line 12 so that the program does not terminate before the alarm time. The alarm is set after 4 seconds (Line 10) and alarm_handler does what the alarm has to do.
Python Signal Processing Summary
It has to be remembered that signals are not same for every operating systems. Some of the signals work in all the operating systems while others don’t. And while working with threads, only the main thread of a process can receive signals. So that’s it for python signal processing. Hope to come up with more tutorials very soon.
Reference: Official Documentation
Hi Pankag,
Can you share PDf Tutorial for same .
Thanks,
Nagireddy