Distilled from [this] 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:
Then, wherever you have a big loop that takes forever to finish, include:
if scriptcontext.escape_test(False): break
: Crashing Python, and need to check for ESC