The signal
module in Python provides mechanisms to use and handle signals. Signals are software interrupts delivered to a process, typically to notify it of events like a user pressing Ctrl+C
(which sends the SIGINT
signal), or a timer expiration (which sends the SIGALRM
signal).
Table of Contents
- Introduction
- Key Functions and Constants
signal.signal
signal.getsignal
signal.pause
signal.setitimer
signal.getitimer
signal.SIG_IGN
signal.SIG_DFL
signal.SIGINT
signal.SIGTERM
- Examples
- Handling
SIGINT
(Ctrl+C) - Using Timers with
SIGALRM
- Ignoring Signals
- Handling Custom Signals
- Handling
- Real-World Use Case
- Conclusion
- References
Introduction
Signals are a way for the operating system to communicate with a process. The signal
module in Python provides an interface to set up handlers for different signals. This is useful for tasks like cleaning up resources on exit, handling timeout events, or dealing with interruptions.
Key Functions and Constants
signal.signal
Sets up a signal handler for a given signal.
import signal
def handler(signum, frame):
print(f'Signal handler called with signal {signum}')
signal.signal(signal.SIGINT, handler)
signal.getsignal
Returns the current signal handler for a given signal.
current_handler = signal.getsignal(signal.SIGINT)
signal.pause
Pauses the program until a signal is received.
signal.pause()
signal.setitimer
Sets an interval timer.
signal.setitimer(signal.ITIMER_REAL, 5) # 5 seconds
signal.getitimer
Gets the current value of an interval timer.
remaining_time = signal.getitimer(signal.ITIMER_REAL)
signal.SIG_IGN
A constant to ignore a signal.
signal.signal(signal.SIGINT, signal.SIG_IGN)
signal.SIG_DFL
A constant to reset the signal to its default action.
signal.signal(signal.SIGINT, signal.SIG_DFL)
signal.SIGINT
A constant for the interrupt signal (Ctrl+C).
signal.SIGTERM
A constant for the termination signal.
Examples
Handling SIGINT (Ctrl+C)
import signal
import time
def handler(signum, frame):
print('Signal handler called with signal', signum)
signal.signal(signal.SIGINT, handler)
print('Press Ctrl+C')
while True:
time.sleep(1)
Using Timers with SIGALRM
import signal
import time
def handler(signum, frame):
print('Alarm signal received')
signal.signal(signal.SIGALRM, handler)
signal.setitimer(signal.ITIMER_REAL, 5) # 5 seconds
print('Waiting for alarm...')
signal.pause()
Ignoring Signals
import signal
import time
signal.signal(signal.SIGINT, signal.SIG_IGN)
print('Press Ctrl+C (it will be ignored)')
while True:
time.sleep(1)
Handling Custom Signals
import signal
import os
import time
def handler(signum, frame):
print(f'Received signal {signum}')
signal.signal(signal.SIGUSR1, handler)
print(f'Sending SIGUSR1 to process {os.getpid()}')
os.kill(os.getpid(), signal.SIGUSR1)
time.sleep(1)
Real-World Use Case
Graceful Shutdown of a Server
import signal
import time
import threading
shutdown_event = threading.Event()
def handler(signum, frame):
print('Signal received, shutting down...')
shutdown_event.set()
signal.signal(signal.SIGINT, handler)
signal.signal(signal.SIGTERM, handler)
print('Server is running. Press Ctrl+C to stop.')
while not shutdown_event.is_set():
# Simulate server work
print('Working...')
time.sleep(2)
print('Server has shut down gracefully.')
Conclusion
The signal
module in Python is used for handling asynchronous events such as interrupts and timers. By setting up custom signal handlers, you can manage how your program responds to various signals, making it more robust and responsive to external events.
Comments
Post a Comment
Leave Comment