python - 为什么真正按 Ctrl-C 键使输入功能忽略以前的输入但 SIGINT 或引发 KeyboardInterrupt 不?
问题描述
可重现的代码:
#!/usr/bin/python3
import time
import os
import signal
try:
time.sleep(5)
#raise KeyboardInterrupt or send SIGINT to this program itself
os.kill(os.getpid(),signal.SIGINT)
except KeyboardInterrupt:
print("*"+input("e:\n"))
输入一些东西,然后等待 SIGINT 或 KeyboardInterrupt 异常的超时:
$ ./test.py
within timeout
e:
*within timeout
输入一些东西然后立即按下 Ctrl-C 导致一个块等待用户输入并且之前输入的内容被忽略:
$ ./test.py
within timeout
^Ce:
after real Ctrl-C
*after real Ctrl-C
为什么会有这种差异?
我想这与“^C”(文本结尾字符)和处理“^C”控制字符的输入函数的实现有关。
我的猜测正确吗?我也想按需切换这些行为。
解决方案
推荐阅读
- c++ - 谷歌模拟委托假必须是可复制构造的
- microsoft-cognitive - 无法使用 Microsoft CustomSpeech 服务训练模型
- c++ - 如何在结构中正确重载 < 运算符以启用在 C++ 中的集合中使用
- python - 您如何阅读文本文件并仅打印一起列出的某些行?
- node.js - 通过 Firebase SDK 节点生成重置密码电子邮件时出错
- regex - 从 .ini 文件中读取正则表达式
- python - 如何制作标记数据点的分布图?
- java - Apache POI 确定最后一行的可靠方法
- r - 获取树库中数据的终端节点
- json - 如何将 api 调用结果导出到具有单响应值的 csv 中?