Stop Sublime Text from executing infinite loop
Question:
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.
Is there a way of setting some kind of 'max_execution_time'
or any other workaround which allow us to stop this nicely?
Answers:
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
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
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
You want to use Ctrl+Break. For your own information, just go check under Tools in Sublime Text and you’ll see Cancel Build and the above hotkey. It’ll work just fine for infinite loops. Suffice to say, I’ve had the same happen! 😉
For Windows users, there is no Break key, so go into Preferences>Key Bindings and change the line
{ "keys": ["ctrl+break"], "command": "cancel_build" }
to a different shortcut, such as Ctrl+Alt+B
For MacOS:
cmd + option + esc
to force quit
The combination is ctrl+break
.
In Windows there is no break button, so you can go to Preferences > Key Bindings
and to the user side add this:
{ "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.
UPDATE
As @Brad mentioned, Ctrl+C will override the copy keyboard shortcut. It is wise to assign another key to it, such as Q as Brad pointed out.
Just type CTRL+C key on MacOS.
Just follow this in case you are using Sublime Text 3, go to 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).
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.
Is there a way of setting some kind of 'max_execution_time'
or any other workaround which allow us to stop this nicely?
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
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
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
You want to use Ctrl+Break. For your own information, just go check under Tools in Sublime Text and you’ll see Cancel Build and the above hotkey. It’ll work just fine for infinite loops. Suffice to say, I’ve had the same happen! 😉
For Windows users, there is no Break key, so go into Preferences>Key Bindings and change the line
{ "keys": ["ctrl+break"], "command": "cancel_build" }
to a different shortcut, such as Ctrl+Alt+B
For MacOS:
cmd + option + esc
to force quit
The combination is ctrl+break
.
In Windows there is no break button, so you can go to Preferences > Key Bindings
and to the user side add this:
{ "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.
UPDATE
As @Brad mentioned, Ctrl+C will override the copy keyboard shortcut. It is wise to assign another key to it, such as Q as Brad pointed out.
Just type CTRL+C key on MacOS.
Just follow this in case you are using Sublime Text 3, go to 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).