Python - 停止无限循环的键盘命令? [英] Python - A keyboard command to stop infinite loop?
问题描述
可能的重复:
为什么我不能在python中处理KeyboardInterrupt?
我在玩一些 Python 代码并创建了一个无限循环:
I was playing around with some Python code and created an infinite loop:
y = 0
x = -4
itersLeft = x
while(itersLeft<0):
y = y + x
itersLeft = itersLeft - 1
print "y = ",y, "itersLeft = ", itersLeft
print y
是否有允许我停止循环的键盘快捷键 - 允许我修复循环然后重新启动它?
Is there a keyboard shortcut that would allow me to stop the looping - allowing me to fix the loop and then restart it?
我试过 Ctrl+C 但没有任何运气.如果它有帮助,我正在使用 Windows 7 环境.
I've tried Ctrl+C and didn't have any luck. If it helps I'm using a Windows 7 environment.
谢谢.
编辑
我还应该提到我正在使用 Aptana Studio 3 并尝试在其中运行 Ctrl+C 命令.它在那里不起作用 - 但在常规控制台中尝试它可以正常工作.我假设它一定是因为 Aptana 环境.
I should have also mentioned that I'm using Aptana Studio 3 and attempted to run the Ctrl+C command within that. It doesn't work there - but trying it within the regular console works fine. I'm assuming it must be because of the Aptana environment.
推荐答案
Ctrl+C 正是您所需要的.如果它不起作用,请加大力度.:-) 当然,你也可以直接关闭shell窗口.
Ctrl+C is what you need. If it didn't work, hit it harder. :-) Of course, you can also just close the shell window.
你没有提到情况.作为最后的手段,您可以编写一个包含 taskkill/im python.exe
的批处理文件,并将其放在您的桌面、开始菜单等上,并在您需要杀死失控的脚本时运行它.当然,它会杀死所有 Python 进程,所以要小心.
You didn't mention the circumstances. As a last resort, you could write a batch file that contains taskkill /im python.exe
, and put it on your desktop, Start menu, etc. and run it when you need to kill a runaway script. Of course, it will kill all Python processes, so be careful.
这篇关于Python - 停止无限循环的键盘命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!