简体   繁体   English

循环时无法键盘中断

[英]Unable to KeyboardInterrupt while loop

I have a python script that runs on an infinite loops.我有一个在无限循环上运行的 python 脚本。 In the loop multiprocess and async functions happen so normally I catch KeyboardInterrupt to properly kill all the processes.在循环中多进程和异步函数发生,所以通常我会捕获 KeyboardInterrupt 以正确终止所有进程。

Using similar code somehow on one of the loops I am unable to catch the KeyboardInterrupt the loop just keeps going.在其中一个循环上以某种方式使用类似的代码我无法捕捉到 KeyboardInterrupt 循环只是继续。

logic goes like that:逻辑是这样的:

try:
    while True:
         do stuff
except (KeyboardInterrupt, SystemExit):
    exit cleanly

Normally I would suspect a blanket try ... except somewhere in the children functions but I went over the whole code base and while there is a lot of error catching everything is specific.通常我会怀疑一个全面的尝试......除了子函数中的某个地方,但我浏览了整个代码库,虽然有很多错误捕捉到一切都是特定的。

Is there a way to trace errors and somehow figure out where the KeyboardInterrupt is caught ?有没有办法跟踪错误并以某种方式找出 KeyboardInterrupt 被捕获的位置?

Thank you谢谢

****** Edit after some debugging... So I disabled the code part by part until I cornered the bug: Somewhere in the code I was calling a method that was missing self and was not marked as @staticmethod. ****** 在一些调试后进行编辑...所以我部分地禁用了代码,直到我解决了这个错误:在代码中的某个地方,我正在调用一个缺少 self 并且没有标记为 @staticmethod 的方法。

Changing that fixed my issue.改变它解决了我的问题。

This works for me.这对我有用。

try:
    while True:
        print(1)
except KeyboardInterrupt:
    raise

EDIT: Actually read your question, I won't be able to tell you why it's not raising an error without seeing the rest or more of the code.编辑:实际上阅读您的问题,我将无法告诉您为什么在没有看到其余或更多代码的情况下它不会引发错误。

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

相关问题 python退出无限循环与KeyboardInterrupt异常 - python exit infinite while loop with KeyboardInterrupt exception 在while循环中生成绘图后退出KeyboardInterrupt - exit on KeyboardInterrupt after generating plots in while loop 为什么虽然True循环不会被Pycharm上的`KeyboardInterrupt`中断? - Why While True loop does not interrupt by `KeyboardInterrupt` on Pycharm? Python 键盘中断不停止循环 - Python KeyboardInterrupt not stopping loop KeyboardInterrupt需要一段时间 - KeyboardInterrupt taking a while 在使用 KeyboardInterrupt 退出之前完成循环 - Finish loop before exiting with a KeyboardInterrupt 如何在不使用 KeyboardInterrupt 的情况下从按键退出 while 循环? [Python] - How do I exit a while loop from a keypress without using KeyboardInterrupt? [python] 使用 KeyboardInterrupt 退出循环,然后在 python 中开始另一个循环 - Exit loop with KeyboardInterrupt then begin another loop in python 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? 为什么 KeyboardInterrupt 不输出 if-else 循环? - Why is KeyboardInterrupt not outputting an if-else loop?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM