0

There is an answer on SO which explains why Windows 10 does not have signals. Hence, you cannot create custom signal handlers.

Here is code:

import sys
import time
import signal


def _handle_signal(*args, **kwargs):
    print('Signal handler has been called.')
    sys.exit(0)


if __name__ == '__main__':
    signal.signal(signal.SIGINT, _handle_signal)

    while True:
        time.sleep(1)

After I ran it and pressed CTRL + C, _handle_signal has been called. So, windows has signals now? How Python can handle SIGINT signal if there are no signals on Windows?

Python version: 3.10.

  • No, it's a Python approximation of what is similar to a POSIX SIGINT in Windows. You can read the sources for that, btw! – Ulrich Eckhardt May 29 '22 at 14:20
  • Handling Ctrl+C has always been possible... – Anders May 29 '22 at 17:32
  • Windows doesn't have UNIX style signals but does have a couple of special events that are mapped to signals. See, for example, https://learn.microsoft.com/en-us/windows/console/ctrl-c-and-ctrl-break-signals and https://learn.microsoft.com/en-us/previous-versions/xdkz3x12(v=vs.140) – Kurtis Rader May 29 '22 at 21:35

0 Answers0