I am writing a simple threaded application and i have set the thread as daemon because I wanted my program to exit at KeyboardInterrupt. This works fine and gives expected result with python3 but python2.7 does not seem to respect the daemon flag. Below is my sample code
if __name__ == '__main__':
try:
threads = [Thread(target=some_func, args=(arg1,arg2)) for _ in range(10)]
for th in threads:
th.daemon=True
th.start()
for th in threads:
th.join()
except KeyboardInterrupt:
sys.exit('Ctrl-c issued by user .. Exiting')
When i run this code on python3 and then hit ctrl-c after a while my program exits as expected, but when i run this with python2.7 and then hit ctrl-c it never exits and I have to kill the process from shell.
Am i missing something here ? I have also tried setting the threading.Event and then clearing the event when KeyboardInterrupt occurs but even that didn't work
With python3 If I don't join my daemon threads then they would exit as soon as the my program is done, and if I don't mark my thread as daemon and don't join then the program continues and as soon as i hit ctrl-c it exits. But none of this works with python2.7
Edit #1
I did some more digging into it and it looks like python2.7 is not able to catch KeyboardInterrupt. This is even more weird because KeyboardInterrupt works fine it is a non-threaded program but with threads the KeyboardInterrupt is not being caught even though the threads are marked as daemon.
All of this happens only on python2.7 and python3 works just fine.