79754245

Date: 2025-09-03 07:15:17
Score: 0.5
Natty:
Report link

When you press Ctrl+C in the terminal, Python raises a KeyboardInterrupt, which you can catch.
But when you press the Stop button in PyCharm, PyCharm doesn’t send KeyboardInterrupt. Instead, it terminates the process by sending a signal — usually SIGTERM (terminate) or sometimes SIGKILL (kill).

Here’s a small example:

import signal
import sys
import time

a = 0

def cleanup(signum, frame):
    global a
    print("Cleaning up before exit...")
    a = 0
    sys.exit(0)

# Handle Ctrl+C and PyCharm's Stop button
signal.signal(signal.SIGINT, cleanup)
signal.signal(signal.SIGTERM, cleanup)

a = 1
print("Loop started...")
while True:
    time.sleep(1)
Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Starts with a question (0.5): When you
  • Low reputation (1):
Posted by: Pedram Majidi