在 QNX 上运行 python 时,我遇到捕获键盘中断的问题。代码如下。它在 Windows 上运行良好,但在 QNX 上运行时,在代码中引发 KeyboardInterrupt 异常时出现以下错误:“信号:不支持”
当我在 QNX 上运行任何 python 脚本并输入 Ctrl+C 时,脚本只是退出,没有异常或错误消息打印到终端,并且代码中没有带有 KeyboardInterrupt 异常的 try/ except 块捕获它。
操作系统:QNX 7.1 Python:v3.11.2
代码:
try:
while True:
time.sleep(1)
raise KeyboardInterrupt("test interrupt")
except BaseException as ex:
print("here")
print(ex)
print()
traceback.print_exc()
raise ex
QNX 输出
here
test interrupt
Traceback (most recent call last):
File "/TestKeyboardInterrupt.py", line 69, in <module>
raise KeyboardInterrupt("test interrupt")
KeyboardInterrupt: test interrupt
Traceback (most recent call last):
File "/TestKeyboardInterrupt.py", line 75, in <module>
raise ex
File "/TestKeyboardInterrupt.py", line 69, in <module>
raise KeyboardInterrupt("test interrupt")
KeyboardInterrupt: test interrupt
signal: Not supported
我的问题是:
您尝试过用
except KeyboardInterrupt
代替raise
吗?
因为当你直接引发异常时,你的代码总是进入异常阶段。
try:
while True:
time.sleep(1)
except KeyboardInterrupt("test interrupt") as ex:
print("here")
print(ex)
print()
traceback.print_exc()
raise ex