3

I have following test code executed on 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 )

If i press Ctrl-C while it's working, i see two KeyboardInterrupt exceptions - one for sleep( 1 ) and one for sleep( 2 ). How it's happens that keyboard interrupt in main process is forwarded to child process? They are processes after all, not threads :(.

grigoryvp
  • 40,413
  • 64
  • 174
  • 277

1 Answers1

2

The KeyboardInterrupt exception is thrown when a process catches the SIGINT signal which indicates a keyboard interrupt (pressing ctrl+c).

In Unix/Linux systems the SIGINT signal is sent to the entire foreground process group which includes the parent process and its child processes.

  • But i'm running this code on Windows. Is it same logic with Ctrl-C on windows even without `SIGTERM`? – grigoryvp Jan 01 '13 at 22:21
  • Unsure. You should add the tag for your specific version of Windows... it might be relevant to the answer you get. Also, the signal is `SIGINT` (keyboard interrupt) _not_ `SIGTERM`. –  Jan 01 '13 at 22:28