![为什么 Ctrl + C 不能终止终端中运行的 Python?](https://linux22.com/image/1017208/%E4%B8%BA%E4%BB%80%E4%B9%88%20Ctrl%20%2B%20C%20%E4%B8%8D%E8%83%BD%E7%BB%88%E6%AD%A2%E7%BB%88%E7%AB%AF%E4%B8%AD%E8%BF%90%E8%A1%8C%E7%9A%84%20Python%EF%BC%9F.png)
我试图理解为什么当我在无限循环中按下 Ctrl + C 时整个 Python 进程不会被终止,或者就此而言,我在终端中运行的任何 Python 函数都不会被终止,而只有循环/函数被停止?
答案1
答案2
值得从文档中指出的是,您需要KeyboardInterrupt
明确处理,即使您已经有了Exception
:
The exception inherits from BaseException so as to not be accidentally caught by code that catches Exception and thus prevent the interpreter from exiting.