How to cancel script execution

Distilled from [this][1] post:

If your python script is taking too long to execute, and you want to cancel it, you have to specifically test if the escape key has been pressed. Otherwise, you’ll just have to wait until it’s done, which can be a very long time.

At the top of your script:

import scriptcontext

Then, wherever you have a big loop that takes forever to finish, include:

    if scriptcontext.escape_test(False):
        break

Enjoy!
[1]: Crashing Python, and need to check for ESC