2013-01-01 171 views
2

我有以下在Windows上執行測試代碼:Python:雖然KeyboardInterrupt被轉發到多進程子進程?

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?它們是方法畢竟不是線程:(。當一個進程catches the SIGINT signal,其指示a keyboard interrupt(按CTRL + C)。

在Unix/Linux系統的SIGINT信號被髮送到

+0

這類事情依賴於底層的操作系統,所以你也應該標記你運行這個什麼操作系統。 – Keith

+0

@Keith新增的「窗口」標籤。 – grigoryvp

回答

1

KeyboardInterrupt拋出異常整個foreground process group,其中包括父進程及其子進程。

+0

但我在Windows上運行這些代碼,甚至是在Windows上與Ctrl-C相同的邏輯沒有'SIGTERM'? – grigoryvp

+0

不確定。你應該爲你特定版本的Windows添加標籤......它可能與你得到的答案有關。此外,信號是'SIGINT'(鍵盤中斷)_not_'SIGTERM'。 – 2013-01-01 22:28:09