Search code examples
pythonpython-multithreading

'ctrl + c' doesn't work with this python code on windows


In my mac, ctrl + c works very well with this python code. So the script exit with KeyboardInterrupt exception.

But in windows 10 however, ctrl + c doesn't work at all. So the script runs forever.

I don't know what is problem.

Here is my code:

import time
import threading


def fast_scrap():
    pass


def slow_scrap_thread(keyword_list: list):
    sleep_time = 2
    for keyword in keyword_list:
        print(keyword)
        print("sleep " + str(sleep_time) + "secs")
        time.sleep(sleep_time)


def slow_scrap():
    rounds = 0
    while True:
        keyword_list = ['q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p']
        keyword_lists = list()
        threads = list()
        for i in range(len(keyword_list) // 3 + 1):
            keyword_lists.append(keyword_list[i * 3:i * 3 + 3])
        for keyword_list in keyword_lists:
            thread = threading.Thread(target=slow_scrap_thread, args=(keyword_list, ))
            # thread.daemon = True
            thread.start()
            threads.append(thread)
        for thread in threads:
            thread.join()
        print("rounds: " + str(rounds))
        time.sleep(3)
        rounds += 1


def main():
    thread0 = threading.Thread(target=fast_scrap)
    # thread0.daemon = True
    thread0.start()
    thread1 = threading.Thread(target=slow_scrap)
    # thread1.daemon = True
    thread1.start()


if __name__ == "__main__":
    main()

I think something's wrong with threading in my code, but not sure what it is it.

--------------------edit---------------------

Sorry here is more minimal code:

import time
import threading


def test_func():
    rounds = 0
    while True:
        print("test_func")

        print("rounds: " + str(rounds))
        time.sleep(3)
        rounds += 1


def main():
    thread0 = threading.Thread(target=test_func)
    # thread0.daemon = True
    thread0.start()


if __name__ == "__main__":
    main()


and i'm running this code in cmd using conda, python 3.9.7

I thought 'thread in thread' makes problem, but the problem seems just in thread.

Sorry but, this problem is only just in my environment??


Solution

  • Couple of things going on here.

    From https://docs.python.org/3/library/signal.html#signals-and-threads:

    Python signal handlers are always executed in the main Python thread of the main interpreter, even if the signal was received in another thread.

    The main thread creates a non-daemon thread and runs test_func() with an infinite loop. The main thread then exits. The non-Daemon thread goes on it way printing a msg then sleeps for 3 seconds, and so on forever.

    Since the interrupt handler is on the main thread, the non-daemon thread keeps running and pressing Ctrl+C has no affect to stop execution.

    However, on Windows you can typically press Ctrl+Pause or Ctrl+ScrLk to terminate the Python process when Ctrl+C doesn't work.

    If code ran test_func() with the sleep call on the main thead, then pressing Ctrl+C would raise a KeyboardInterrupt exception.

    def main():
        test_func()
        # thread0 = threading.Thread(target=test_func)
        # thread0.daemon = True
        # thread0.start()