python-3.xwindowsmultithreadingsigkill

Ctrl+c not stopping a Thread in Windows + python3.7


I'm trying this simple thread with a while loop inside. When I'm inside the while loop, Ctrl+C has no effect in stopping my program. Once I go do something else after the while loop, the script stops as intended. What can I do so my script can be gracefully killed both while being in the while loop and after? (Edit: This seems to be a problem exclusive to Windows, iOS and Ubuntu seem to do what I want)

import time, threading


class MainClass(threading.Thread):

    def __init__(self):
        super().__init__()

    def run(self):
        while True:
            time.sleep(1)
            print("Looping")


# Script entry point
if __name__ == '__main__':

    a = MainClass()
    a.daemon = True
    a.start()
    a.join()

Solution

  • This is a known issue, explained in Issue 35935.

    A way to solve it is to revert to the default kernel behaviour of SIGINT using signal.signal(signal.SIGINT, signal.SIG_DFL), (solution pointed out by the issue OP). As to why this has to be the case is beyond the scope of my knowledge.

    This works on Windows using Python 3.8+.

    Implementation:

    import time, threading
    import signal
    
    
    class MainClass(threading.Thread):
    
        def __init__(self):
            super().__init__()
    
        def run(self):
            while True:
                time.sleep(1)
                print("Looping")
    
    
    # Script entry point
    if __name__ == '__main__':
        a = MainClass()
        a.daemon=True
        signal.signal(signal.SIGINT, signal.SIG_DFL)
        a.start()
        a.join()