I am not sure which operating system and shell you are using. I am describing Mac OS X and Linux using zsh (bash / sh should do the same).
When you press Ctrl + C, all programs running in the foreground in the current terminal receive a SIGINT signal . In your case, this is your main python process and all processes generated by os.system.
The processes spawned by os.system then terminate their execution. Usually, when a python script gets SIGINT, it throws a KeyboardInterrupt exception, but your main process ignores SIGINT due to os.system() . Python os.system() calls the standard C system() function , which causes the call process to ignore SIGINT ( man Linux / man Mac OS X ).
This way none of your python threads gets SIGINT, only children process it.
When you delete the os.system () call, your python process stops ignoring SIGINT and you get KeyboardInterrupt .
You can replace os.system("sleep 10") with subprocess.call(["sleep", "10"]) . subprocess.call() does not force your process to ignore SIGINT.
source share