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