Python 停止 Sublime Text 执行无限循环
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28397473/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Stop Sublime Text from executing infinite loop
提问by Finn
When I do something like
当我做类似的事情时
while True:
print('loop')
and execute that code in sublime I am not able to stop it. I have to manually kill the process and restart sublime.
并在 sublime 中执行该代码我无法阻止它。我必须手动终止进程并重新启动 sublime。
Is there a way of setting some kind of 'max_execution_time'
or any other workaround which allow us to stop this nicely?
有没有办法设置某种'max_execution_time'
或任何其他解决方法,使我们能够很好地停止这种情况?
采纳答案by Eithos
You want to use Ctrl+Break. For your own information, just go check under Tools in Sublime Textand you'll see Cancel Buildand the above hotkey. It'll work just fine for infinite loops. Suffice to say, I've had the same happen! ;)
你想使用Ctrl+ Break。对于你自己的信息,只需在Sublime Text 中的工具下检查,你就会看到取消构建和上面的热键。它适用于无限循环。我只想说,我也遇到过同样的情况!;)
For Windows users, there is no Breakkey, so go into Preferences>Key Bindings and change the line
对于 Windows 用户,没有Break键,所以进入 Preferences>Key Bindings 并更改行
{ "keys": ["ctrl+break"], "command": "cancel_build" }
to a different shortcut, such as Ctrl+Alt+B
到一个不同的快捷方式,例如Ctrl+ Alt+B
回答by Nick Bailey
You have a couple of options here. You could set a huge maximum number of iterations (I actually do this with most while loops until I've completely debugged my code, to avoid infinite loop pains): So for example
你有几个选择。你可以设置一个巨大的最大迭代次数(我实际上对大多数 while 循环都这样做,直到我完全调试了我的代码,以避免无限循环的痛苦):例如
max_iterations = 100000000
while i < max_iterations:
print("Hello World")
An alternative would be using time module to clock the execution time of your code like this
另一种方法是使用 time 模块来计时代码的执行时间,如下所示
import time
max_execution_time = 10000000 #this will be in seconds
start_time = time.clock()
elapsed_time = 0
while elapsed_time < max_execution_time:
elapsed_time = time.clock() = start_time
#Your loop code here
回答by wim
For me (on Linux), there is no break
key on the keyboard and this shortcut was somehow bound to a different combination: ctrl+alt+c.
对我来说(在 Linux 上),break
键盘上没有键,这个快捷方式以某种方式绑定到不同的组合:ctrl+ alt+ c。
You can find where it is bound in the Tools
menu:
您可以在Tools
菜单中找到它的绑定位置:
After interrupting your script you should see the text [Cancelled]
printed to the sublimetext console.
中断脚本后,您应该会看到[Cancelled]
打印到 sublimetext 控制台的文本。
回答by Abhishek Pareek
For MacOS:
对于 MacOS:
cmd + option + esc
to force quit
强制退出
回答by Orestis Zekai
The combination is ctrl+break
.
组合是ctrl+break
。
In Windows there is no break button, so you can go to Preferences > Key Bindings
and to the user side add this:
在 Windows 中没有中断按钮,因此您可以Preferences > Key Bindings
在用户端添加以下内容:
{ "keys" : ["ctrl+c"], "command": "cancel_build"}
{ "keys" : ["ctrl+c"], "command": "cancel_build"}
Now, by pressing Ctrl+C the execution will stop. Of course, you can change the combination to whatever you want.
现在,按 Ctrl+C 将停止执行。当然,您可以将组合更改为您想要的任何内容。
回答by HaSeeB MiR
Just type CTRL+Ckey on MacOS.
只需在MacOS上键入CTRL+C键。
回答by Anurag Pandey
Just follow this in case you are using Sublime Text 3, go to Preferences > Package Settings > Alignment > Key Bindings-User
如果您使用 Sublime Text 3,请按照此操作,转到 Preferences > Package Settings > Alignment > Key Bindings-User
[
{ "keys": ["ctrl+n"], "command": "cancel_build" }
]
Now, by pressing ctrl+n, the execution will immediately stop. Of course, you can change the combination to whatever you want (In place of ctrl+n).
现在,按ctrl+ n,执行将立即停止。当然,您可以将组合更改为您想要的任何组合(代替ctrl+ n)。