我在cygwin外壳中运行python脚本,但是我无法截获 ctrl + c 命令。
这是我的python脚本:
#!/cygdrive/c/python27/python.exe -u
import signal
import sys
def signal_handler(sig, frame):
print('You pressed Ctrl+C!')
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)
signal.signal(signal.SIGBREAK, signal_handler)
print('Press Ctrl+C')
input()
使用try / catch方法仍然无法正常工作
#!/cygdrive/c/python27/python.exe -u
try:
input()
except KeyboardInterrupt:
print('Interrupted')
,但这些处理程序均不能与 ctrl + c 命令一起使用。 Cygwin版本是1.7.25(0.270 / 5/3),我使用的是python 2.7。怎么了?
答案 0 :(得分:0)
我在 mingw64 终端的 MSYS 环境 (https://www.msys2.org/) 中遇到了同样的问题。我终于能够使用 winpty
工具解决这个问题:
winpty python3 my_python_script.py
这种方式信号处理程序按预期工作。我只是不确定您的 cygwin 发行版中是否也提供 winpty
工具。