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 :(.
The
KeyboardInterruptexception is thrown when a process catches theSIGINTsignal which indicates a keyboard interrupt (pressing ctrl+c).In Unix/Linux systems the
SIGINTsignal is sent to the entire foreground process group which includes the parent process and its child processes.