简体   繁体   English

Git Bash 忽略键盘中断控制 Z0D61F8370CAD1D412F80B84D1 中的 Python 脚本

[英]Python script in Git Bash ignores keyboard interrupt Control C

Python script in Git Bash ignores keyboard interrupt Control C when running the script directly. Git Bash 中的 Python 脚本直接运行脚本时忽略键盘中断控制 Z0D61F8370CAD1D412F80B84D14。

This is a simple code to test, called test_sleep_interrupt.py.这是一个简单的测试代码,称为 test_sleep_interrupt.py。 It sleeps 10 times, 1 second each time.它睡10次,每次1秒。 I will type Control C in between.我将在两者之间键入 Control C。

#!/usr/bin/env python
import time
for i in range(0, 10):
    print(f"sleep #{i}")
    time.sleep(1)

When I run the script directly, Control C is ignored当我直接运行脚本时,控制 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

When I run it through python, Control-C works immediately当我通过 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

What happens here?这里会发生什么? Is there a fix to make Control-C work when directly calling the script?直接调用脚本时是否有使 Control-C 工作的修复程序?

I am using Windows 10, Python 3.7.3, Git Bash is mintty-2.9.6我正在使用 Windows 10,Python 3.7.3,Git Z7024FF0860DEC6AB1769Z3 分钟。

Following @ConnorLow's suggestion, I upgraded my Git for Windows to the latest version 2.28.0, which includes Git Bash mintty 3.2.0. Following @ConnorLow's suggestion, I upgraded my Git for Windows to the latest version 2.28.0, which includes Git Bash mintty 3.2.0. This fixed the reported problem.这解决了报告的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM