How do I capture SIGINT in Python on Windows?

2019-02-17 23:52发布

(Similar to this question)

On UNIX under Python 2.7, at the Python prompt:

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

I press ctrl-c

 >>> welcome to the handler

 >>>

On Windows:

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

Upon pressing ctrl-c:

 >>>
 KeyboardInterrupt
 >>>

I can verify that handler is being installed Python-side as the handler for SIGINT (calling signal.signal a second timer returns my handler). How can I capture SIGINT on Windows?

1条回答
小情绪 Triste *
2楼-- · 2019-02-18 00:13

After opening the bug upstream the root cause of the problem was found and a patch was written. This patch won't be going into the python 2.x series.

查看更多
登录 后发表回答