【问题标题】:How do I capture SIGINT in Python on Windows?如何在 Windows 上的 Python 中捕获 SIGINT?
【发布时间】:2013-05-22 08:02:24
【问题描述】:

(类似于this question

在 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>

我按 ctrl-c

 >>> welcome to the handler

 >>>

在 Windows 上:

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

按 ctrl-c 时:

 >>>
 KeyboardInterrupt
 >>>

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

【问题讨论】:

  • 我已经在 Windows Server 2008 R2 上使用 Python 2.6.6 对其进行了测试,上面的代码可以正常工作。这个问题一定是2.7版本才有的。

标签: python windows signals


【解决方案1】:

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

【讨论】:

  • 由于其他几个答案同时引用了这个答案,我觉得有必要指出这个错误最终因为未修复而被关闭。
猜你喜欢
  • 2010-11-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-02
  • 2022-09-23
  • 2021-10-17
  • 1970-01-01
  • 2020-10-19
相关资源
最近更新 更多