且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何通过击键杀死while循环?

更新时间:2023-01-17 16:04:42

最简单的方法是使用通常的 Ctrl-C (SIGINT) 中断它.

The easiest way is to just interrupt it with the usual Ctrl-C (SIGINT).

try:
    while True:
        do_something()
except KeyboardInterrupt:
    pass

由于 Ctrl-C 导致 KeyboardInterrupt 被引发,只需在循环外捕获它并忽略它.

Since Ctrl-C causes KeyboardInterrupt to be raised, just catch it outside the loop and ignore it.