function signal_handler
A signal handler function that gracefully handles SIGINT (Ctrl+C) interrupts by logging a shutdown message and exiting the program cleanly.
/tf/active/vicechatdev/SPFCsync/main.py
25 - 28
simple
Purpose
This function is designed to be registered as a signal handler for SIGINT signals (typically triggered by Ctrl+C). When invoked, it logs an informational message indicating that an interrupt was received and then performs a clean exit with status code 0. This provides graceful shutdown behavior for long-running processes or services that need to handle user interrupts properly.
Source Code
def signal_handler(signum, frame):
"""Handle SIGINT (Ctrl+C) gracefully."""
logging.getLogger(__name__).info("Received interrupt signal, shutting down...")
sys.exit(0)
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
signum |
- | - | positional_or_keyword |
frame |
- | - | positional_or_keyword |
Parameter Details
signum: The signal number that triggered the handler. For SIGINT, this will be the integer value corresponding to signal.SIGINT (typically 2 on Unix-like systems). This parameter is automatically provided by the signal handling mechanism.
frame: The current stack frame object at the point where the signal was received. This is a frame object that can be used for debugging or inspection purposes, though it's not used in this implementation. This parameter is automatically provided by the signal handling mechanism.
Return Value
This function does not return any value. It terminates the program execution by calling sys.exit(0), which raises a SystemExit exception with exit code 0 (indicating successful termination).
Dependencies
loggingsys
Required Imports
import logging
import sys
import signal
Usage Example
import signal
import sys
import logging
# Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
def signal_handler(signum, frame):
"""Handle SIGINT (Ctrl+C) gracefully."""
logging.getLogger(__name__).info("Received interrupt signal, shutting down...")
sys.exit(0)
# Register the signal handler
signal.signal(signal.SIGINT, signal_handler)
# Your main program logic
if __name__ == '__main__':
print("Running... Press Ctrl+C to stop")
try:
while True:
pass # Simulate long-running process
except SystemExit:
print("Exited gracefully")
Best Practices
- Always register this handler using signal.signal(signal.SIGINT, signal_handler) before starting long-running operations
- Ensure logging is properly configured before registering the handler to avoid missing shutdown messages
- Consider adding cleanup logic before sys.exit(0) if your application needs to release resources, close connections, or save state
- Be aware that sys.exit(0) raises SystemExit exception, which can be caught by try-except blocks in the call stack
- For more complex applications, consider using a flag-based approach instead of immediate exit to allow graceful completion of current operations
- This handler only catches SIGINT; consider handling other signals like SIGTERM for more robust shutdown behavior
- The exit code 0 indicates successful termination; use non-zero codes if you need to distinguish between normal and interrupted exits
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function main_v7 46.3% similar
-
class EmailForwardingHandler 36.7% similar
-
function main_v41 36.2% similar
-
function clear_session_v1 35.6% similar
-
function run_command 34.3% similar