Stop Sublime Text from executing infinite loop

前端 未结 7 1750
不知归路
不知归路 2020-12-28 15:36

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

相关标签:
7条回答
  • 2020-12-28 15:47

    Just type CTRL+C key on MacOS.

    0 讨论(0)
  • 2020-12-28 15:48

    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).

    0 讨论(0)
  • 2020-12-28 15:49

    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.

    0 讨论(0)
  • 2020-12-28 15:59

    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
    
    0 讨论(0)
  • 2020-12-28 16:06

    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.

    You can find where it is bound in the Tools menu:

    After interrupting your script you should see the text [Cancelled] printed to the sublimetext console.

    0 讨论(0)
  • 2020-12-28 16:07

    For MacOS:

    cmd + option + esc
    

    to force quit

    0 讨论(0)
提交回复
热议问题