简体   繁体   English

'ctrl + c' 不适用于 Windows 上的此 python 代码

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

In my mac, ctrl + c works very well with this python code.在我的 mac 中, ctrl + c与此 python 代码配合得很好。 So the script exit with KeyboardInterrupt exception.所以脚本退出时出现 KeyboardInterrupt 异常。

But in windows 10 however, ctrl + c doesn't work at all.但是在 Windows 10 中, ctrl + c根本不起作用。 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我正在使用 conda、python 3.9.7 在cmd运行此代码

I thought 'thread in thread' makes problem, but the problem seems just in thread.我认为“线程中的线程”会产生问题,但问题似乎只是在线程中。

Sorry but, this problem is only just in my environment??抱歉,这个问题只出现在我的环境中??

The problem is probably that the threads are being left hanging when you try to use a keyboard interruption on it.问题可能是当您尝试在其上使用键盘中断时线程被挂起。 Try to catch the KeyboardInterrupt exception around where you spawn new threads and join them there.尝试在产生新线程的地方捕获KeyboardInterrupt 异常并在那里加入它们。

Couple of things going on here.这里发生了几件事。

From https://docs.python.org/3/library/signal.html#signals-and-threads :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. Python 信号处理程序始终在主解释器的主 Python 线程中执行,即使信号是在另一个线程中接收到的。

The main thread creates a non-daemon thread and runs test_func() with an infinite loop.主线程创建一个非守护线程并无限循环运行 test_func()。 The main thread then exits.然后主线程退出。 The non-Daemon thread goes on it way printing a msg then sleep for 3 seconds, and so on forever.非守护进程线程继续打印 msg 然后休眠 3 秒,依此类推。

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.由于中断处理程序在主线程上,非守护线程继续运行,按 Ctrl+C 不会影响停止执行。

However, on Windows you can typically press Ctrl+Pause or Ctrl+ScrLk to terminate the Python process when Ctrl+C doesn't work.但是,在 Windows 上,当Ctrl+C不起作用时,您通常可以按Ctrl+PauseCtrl+ScrLk来终止 Python 进程。

If code ran test_func() with the sleep call on the main thead, then pressing Ctrl+C would raise a KeyboardInterrupt exception.如果代码运行 test_func() 并在主线程上调用 sleep ,则按 Ctrl+C 将引发 KeyboardInterrupt 异常。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM