简体   繁体   English

python 在无限循环期间如何处理键盘中断?

[英]How does python handle KeyboardInterrupt during infinite loop?

When I run this code:当我运行此代码时:

while 1:
    try:
        pass
    except KeyboardInterrupt:
        pass

and I press ctrl+c to try to terminate the program through a KeyboardInterrupt, I am usually, but not always successful.我按ctrl+c尝试通过 KeyboardInterrupt 终止程序,我通常是,但并不总是成功。 Most of the time, my first attempt at ctrl+c terminates the program, but sometimes, I need to press ctrl+c twice.大多数时候,我第一次尝试ctrl+c会终止程序,但有时我需要按ctrl+c两次。

Compare that with this code:将其与以下代码进行比较:

from time import sleep

while 1:
    try:
        sleep(0.000000000000000000000000000000000000000000000000000000000000000000001)
    except KeyboardInterrupt:
        pass

With this code, no matter how many times I press ctrl+c , the program never terminates.使用此代码,无论我按多少次ctrl+c ,程序都不会终止。

My assumption is that in the first scenario, my KeyboardInterrupt usually works because the pass statements executes so quickly that I am more likely to press ctrl+c during the while loop condition check (which is not in the try block) than during the pass statement execution (which is in the try block).我的假设是,在第一种情况下,我的 KeyboardInterrupt 通常会起作用,因为pass语句执行得如此之快,以至于我更有可能在 while 循环条件检查(不在 try 块中)而不是在 pass 语句期间按ctrl+c执行(在 try 块中)。

And, in the second example, I assume the sleep function must take much longer to execute than the while loop condition check such that I am almost guaranteed to press ctrl+c during its execution (thus catching the KeyboardInterrupt in the try block and continuing the loop).而且,在第二个示例中,我假设 sleep function 必须比 while 循环条件检查花费更长的时间来执行,这样我几乎可以保证在其执行期间按ctrl+c (因此在 try 块中捕获 KeyboardInterrupt 并继续环形)。

Can anyone confirm my assumption or give an alternate reasoning?谁能证实我的假设或给出替代推理?

Since you are pass ing the keyboard interrupt in both cases, the program just continues with the while loop.由于您在这两种情况下都pass了键盘中断,因此程序将继续执行while循环。

With the second case, due to the sleep in the body of the try block, there is a mu8ch higher chance of your KeyboardInterrupt hitting the process during that sleep - which means your except block is almost guaranteed to be executed.对于第二种情况,由于try块的主体中的sleep ,您的KeyboardInterrupt在该sleep期间击中进程的可能性要高得多 - 这意味着您的except块几乎可以保证被执行。 Which means that your program just carries on.这意味着你的程序只是继续。 You can test this by adding a print in the except block.您可以通过在except块中添加打印来测试这一点。

from time import sleep

while 1:
    try:
        sleep(0.000000000000000000000000000000000000000000000000000000000000000000001)
    except KeyboardInterrupt:
        print('exc')
        pass

If I run this and press Ctrl+C I get:如果我运行它并按Ctrl+C我得到:

>python so_test.py
exc
exc
exc
exc
exc
exc
exc
exc

With each Ctrl+C the except block is executed, but your program continues on.每个Ctrl+C都会执行except块,但您的程序会继续执行。

In the first first case however, due to the lack of any delay (ie sleep ) in the try block, the programs is just as likely to be executing outside the try block (before entering it, or after) when the KeyboardInterrupt hits your process.然而,在第一种情况下,由于try块中没有任何延迟(即sleep ),当KeyboardInterrupt击中您的进程时,程序很可能在try块之外(进入之前或之后)执行. Which means that your exception is not handled, and that ends the program.这意味着您的异常未得到处理,并且程序结束。 If however, the interrupt hits the process inside the try -block, the program will continue.但是,如果中断命中try块内的进程,则程序将继续。 Check this:检查这个:

while 1:
    try:
        pass
    except KeyboardInterrupt:
        print('exc')
        pass

And you can get this:你可以得到这个:

>python so_test.py
exc
exc
exc
exc
Traceback (most recent call last):
  File "so_test.py", line 5, in <module>
    pass
KeyboardInterrupt

Hope that clears up the confusion.希望这能消除混乱。

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

相关问题 python退出无限循环与KeyboardInterrupt异常 - python exit infinite while loop with KeyboardInterrupt exception Python 键盘中断不停止循环 - Python KeyboardInterrupt not stopping loop Python如何手动结束收集数据的无限while循环,而不是结束代码而不使用KeyboardInterrupt? - Python how can I manually end an infinite while loop that's collecting data, without ending the code and not using KeyboardInterrupt? Python如何处理无限递归? - How does Python handle infinite recursion? 如何避免在python垃圾收集期间抑制KeyboardInterrupt? - How to avoid suppressing KeyboardInterrupt during garbage collection in python? 如何在 Python 中的请求期间处理错误循环? - How to handle Loop for error during Requests in Python? 如何在python中忽略KeyboardInterrupt? - How to ignore KeyboardInterrupt in python? 如何通过键盘绑定或宏退出Python程序或循环? 键盘中断不起作用 - How to exit a Python program or loop via keybind or macro? Keyboardinterrupt not working 如何在 python 中处理 KeyboardInterrupt 后返回执行循环 - how to return to executing loop after handling KeyboardInterrupt in python Python套接字,如何摆脱无限循环和处理异常 - Python sockets, how to escape from infinite loop and handle exceptions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM