[英]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.