【问题标题】:Python: while KeyboardInterrupt is forwarded to multiprocessing child process?Python:而 KeyboardInterrupt 被转发到多处理子进程?
【发布时间】:2012-12-16 08:53:36
【问题描述】:

我在 Windows 上执行了以下测试代码:

import multiprocessing
import time

def child() :
  while True :
    time.sleep( 2 )

if __name__ == '__main__' :
  multiprocessing.Process( target = child ).start()
  while True :
    time.sleep( 1 )

如果我在工作时按Ctrl-C,我会看到两个 KeyboardInterrupt 异常 - 一个用于sleep( 1 ),一个用于sleep( 2 )。主process 中的键盘中断是如何转发给子process 的?毕竟它们是进程,而不是线程 :(.

【问题讨论】:

  • 这类事情取决于底层操作系统,因此您还应该标记您正在运行它的操作系统。
  • @Keith 添加了“windows”标签。

标签: python windows windows-8 64-bit multiprocessing


【解决方案1】:

当进程 catches the SIGINT signal 指示 a keyboard interrupt(按 ctrl+c)时,会引发 KeyboardInterrupt 异常。

在 Unix/Linux 系统中,SIGINT 信号被发送到整个 foreground process group,包括父进程及其子进程。

【讨论】:

  • 但我在 Windows 上运行此代码。即使没有SIGTERM,Windows 上的 Ctrl-C 逻辑是否相同?
  • 不确定。您应该为您的特定 Windows 版本添加标签......它可能与您得到的答案有关。另外,信号是SIGINT(键盘中断)not SIGTERM.
猜你喜欢
  • 1970-01-01
  • 2010-10-21
  • 1970-01-01
  • 2017-08-29
  • 2021-05-01
  • 2021-07-16
  • 2017-01-22
  • 1970-01-01
  • 2016-10-08
相关资源
最近更新 更多