Hi!
When testing while loops, its is quite common practice to build in a break statement after a set number of iterations:
breakingPoint = 5000
iterCount = 0
while <condition>:
<logic>
iterCount = iterCount +1
if iterCount == breakingPoint:
break
<statement>
If whatever comes after your while loop doesn't have a chance of success whitout the loop completing you can also use an error message:
import maya.cmds as cmds
breakingPoint = 5000
iterCount = 0
while <condition>:
<logic>
iterCount = iterCount +1
if iterCount == breakingPoint:
cmds.error("While loop ran for too long")
<statement>
This gives you all the benefits of the while loop, as it stops early if the condition turns true but it also allows you to set an end to it automatically.
Ofcourse there is the slight chance that your while loop would have completed after the breakoff point, so I would chose the breakoff point rather high. Also you'll have to wait for the loop to reach the breakoff point, but that is normally still faster than terminating and reopening maya.
I hope it helps!