How to capture SIGINT in Python?

How to capture SIGINT in Python?

You can capture the SIGINT (Ctrl+C) signal in Python using the signal module. The signal module allows you to handle signals, including SIGINT, which is typically used to interrupt the execution of a Python script. Here's how you can capture SIGINT and perform custom actions when it is received:

import signal import sys # Define a custom signal handler function def sigint_handler(signal, frame): print("\nSIGINT received. Exiting gracefully.") # Perform any cleanup or additional actions here if needed sys.exit(0) # Register the signal handler for SIGINT signal.signal(signal.SIGINT, sigint_handler) # Your main code here print("Press Ctrl+C to trigger SIGINT.") while True: # Your main code loop pass 

In the code above:

  1. We import the signal and sys modules.

  2. We define a custom signal handler function named sigint_handler. This function will be called when a SIGINT signal (Ctrl+C) is received.

  3. We register our custom signal handler using signal.signal(signal.SIGINT, sigint_handler). This associates the SIGINT signal with our sigint_handler function.

  4. Inside your main code, you can add the logic that you want to execute. In this example, we have a simple loop that continues indefinitely. You can replace this with your actual application logic.

When you run this code and press Ctrl+C in the terminal, the custom sigint_handler function will be called, and you can perform any necessary cleanup or additional actions before exiting the script gracefully.

Examples

  1. "Python SIGINT handler example"

    • Description: This query seeks an example of how to implement a signal handler in Python specifically for handling the SIGINT signal.
    • Code:
      import signal import sys def sigint_handler(signal, frame): print('SIGINT received. Exiting gracefully...') sys.exit(0) signal.signal(signal.SIGINT, sigint_handler) # Your main program logic here 

    This code sets up a SIGINT handler that prints a message when the SIGINT signal is received and exits gracefully.

  2. "Python SIGINT handling with KeyboardInterrupt"

    • Description: This query explores how Python handles SIGINT signals in the context of KeyboardInterrupt exceptions.
    • Code:
      import signal def sigint_handler(signal, frame): print('SIGINT received. Exiting gracefully...') signal.signal(signal.SIGINT, sigint_handler) try: # Your main program logic here pass except KeyboardInterrupt: print('KeyboardInterrupt received. Exiting...') exit(0) 

    This code sets up a SIGINT handler and catches KeyboardInterrupt exceptions, providing a controlled exit.

  3. "Python catch SIGINT and continue"

    • Description: This query aims to find how to capture SIGINT signals without halting the program's execution.
    • Code:
      import signal def sigint_handler(signal, frame): print('SIGINT received. Continuing execution...') signal.signal(signal.SIGINT, sigint_handler) # Your main program logic here 

    This code sets up a SIGINT handler that prints a message upon signal reception but allows the program to continue execution.

  4. "Python ignore SIGINT signal"

    • Description: This query looks for ways to make Python ignore SIGINT signals.
    • Code:
      import signal signal.signal(signal.SIGINT, signal.SIG_IGN) # Your main program logic here 

    This code sets the SIGINT signal handler to SIG_IGN, causing Python to ignore SIGINT signals.

  5. "Python SIGINT handler not working"

    • Description: This query addresses issues with SIGINT handlers not functioning as expected.
    • Code:
      import signal import sys def sigint_handler(signal, frame): print('SIGINT handler is not working properly.') signal.signal(signal.SIGINT, sigint_handler) # Your main program logic here 

    This code sets up a SIGINT handler that should be working but might need further debugging to resolve issues.

  6. "Python graceful shutdown SIGINT"

    • Description: This query focuses on implementing a graceful shutdown procedure upon receiving a SIGINT signal.
    • Code:
      import signal import sys def sigint_handler(signal, frame): print('SIGINT received. Performing graceful shutdown...') # Add your cleanup and shutdown procedures here sys.exit(0) signal.signal(signal.SIGINT, sigint_handler) # Your main program logic here 

    This code sets up a SIGINT handler that triggers a graceful shutdown process upon signal reception.

  7. "Python SIGINT handler logging"

    • Description: This query looks for examples of logging events when a SIGINT signal is received.
    • Code:
      import signal import logging logging.basicConfig(level=logging.INFO) def sigint_handler(signal, frame): logging.info('SIGINT received. Logging event...') # Add your cleanup and shutdown procedures here signal.signal(signal.SIGINT, sigint_handler) # Your main program logic here 

    This code sets up a SIGINT handler that logs an event upon signal reception.

  8. "Python handle multiple signals"

    • Description: This query explores how to handle multiple signals, including SIGINT, in Python.
    • Code:
      import signal import sys def sigint_handler(signal, frame): print('SIGINT received. Exiting gracefully...') sys.exit(0) def sigterm_handler(signal, frame): print('SIGTERM received. Exiting gracefully...') sys.exit(0) signal.signal(signal.SIGINT, sigint_handler) signal.signal(signal.SIGTERM, sigterm_handler) # Your main program logic here 

    This code sets up handlers for both SIGINT and SIGTERM signals, enabling graceful exits for each.

  9. "Python signal module documentation"

    • Description: This query aims to find official documentation for Python's signal module for handling signals.
    • Code:
      import signal import webbrowser webbrowser.open('https://docs.python.org/3/library/signal.html') 

    This code opens the official documentation for Python's signal module in a web browser.

  10. "Python handle SIGINT and run cleanup"

    • Description: This query seeks methods to handle SIGINT signals and execute cleanup routines.
    • Code:
      import signal import sys def sigint_handler(signal, frame): print('SIGINT received. Running cleanup...') # Add your cleanup procedures here sys.exit(0) signal.signal(signal.SIGINT, sigint_handler) # Your main program logic here 

    This code sets up a SIGINT handler that runs cleanup routines before exiting the program.


More Tags

unsatisfiedlinkerror wsimport timedelay C# sampling c#-5.0 ntext c99 size mayavi

More Python Questions

More Pregnancy Calculators

More Auto Calculators

More Housing Building Calculators

More Animal pregnancy Calculators