如何在Windows上捕获Python中的SIGINT?

ldr*_*drg 4 python windows signals

(与此问题类似)

在Python 2.7下的UNIX上,在Python提示符下:

 >>> import signal
 >>> def handler(signal, frame):
 ...     print 'welcome to the handler'
 ...
 >>> signal.signal(signal.SIGINT, handler)
 <built-in function default_int_handler>
Run Code Online (Sandbox Code Playgroud)

我按ctrl-c

 >>> welcome to the handler

 >>>
Run Code Online (Sandbox Code Playgroud)

在Windows上:

 >>> import signal
 >>> def handler(signal, frame):
 ...     print 'welcome to the handler'
 ...
 >>> signal.signal(signal.SIGINT, handler)
 <built-in function default_int_handler>
Run Code Online (Sandbox Code Playgroud)

按ctrl-c:

 >>>
 KeyboardInterrupt
 >>>
Run Code Online (Sandbox Code Playgroud)

我可以验证handler正在安装Python端作为SIGINT的处理程序(调用signal.signal第二个计时器返回我的handler).如何在Windows上捕获SIGINT?

ldr*_*drg 9

在打开上游错误后,找到了问题的根本原因并编写了补丁.这个补丁不会进入python 2.x系列.

  • 由于其他几个答案同时引用了这个答案,我觉得有必要指出这个错误最终因未修复而被关闭。 (2认同)