【发布时间】:2020-08-07 01:07:29
【问题描述】:
Git Bash 中的 Python 脚本在直接运行脚本时会忽略键盘中断 Control C。
这是一个简单的测试代码,称为 test_sleep_interrupt.py。它睡10次,每次1秒。我将在两者之间键入 Control C。
#!/usr/bin/env python
import time
for i in range(0, 10):
print(f"sleep #{i}")
time.sleep(1)
当我直接运行脚本时,控件 C 被忽略
$ ./test_sleep_interrupt.py
sleep #0
sleep #1
sleep #2
(hitting Control-C many times, no effect)
sleep #3
sleep #4
sleep #5
sleep #6
sleep #7
sleep #8
sleep #9
当我通过 python 运行它时,Control-C 立即起作用
$ python ./test_sleep_interrupt.py
sleep #0
sleep #1
(typed Control-C)
Traceback (most recent call last):
File "./test_sleep_interrupt.py", line 5, in <module>
time.sleep(1)
KeyboardInterrupt
这里发生了什么?直接调用脚本时是否有解决方案使 Control-C 工作?
我使用的是 Windows 10,Python 3.7.3,Git Bash 是 mintty-2.9.6
【问题讨论】:
-
听起来像this SO question
-
@ConnorLow。我尝试了您链接中的技巧。为 Windows 升级 Git 解决了这个问题。我会发布解决方案
-
@ConnorLow,谢谢你的建议。本来我以为是和Python内部有关
标签: python bash git keyboardinterrupt control-c