Stop Sublime Text from executing infinite loop

前端 未结 7 1844
不知归路
不知归路 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: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
    

提交回复
热议问题