Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: while KeyboardInterrupt is forwarded to multiprocessing child process?

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 :(.

like image 914
grigoryvp Avatar asked Jul 10 '26 07:07

grigoryvp


1 Answers

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.