20

How does one break the execution of a long-running process in the python console of ArcMap?

For example, how do you break a long iteration like the one below once it is started?

for i in range(1, 50):
  # some long process which takes over a minute to complete

I want to stop the process at any time I want by pressing a keyboard combination, clicking a button or something similar while the code I have input in the console window is running.

5
  • 1
    By break do you mean do you mean force it to quit?
    – R.K.
    Commented Dec 20, 2012 at 11:58
  • @R.K.: Yes, I want to force it to quit at any time I want, because I have (1) seen an undesired output, (2) have entered the wrong processing order, (3) any other reason. Commented Dec 20, 2012 at 12:57
  • 2
    Just checked the possible keyboard shortcuts. Ctrl + Z, Ctrl + C and ESC all don't work. You might need to use IDLE or another IDE to accomplish what you want.
    – R.K.
    Commented Dec 20, 2012 at 13:02
  • use ctrl +Z in lx terminal for windows machine
    – user60221
    Commented Oct 4, 2015 at 7:20
  • @anurag: Thank you for your suggestion but that is not what I desire. What I want is to stop the execution inside the Python window in ArcMap, not using an IDE or Terminal. I already know how to terminate execution in a terminal or IDE. Commented Oct 16, 2015 at 11:59

8 Answers 8

14

Just tested it and ESC doesn't work either. ArcGIS just freezes for a moment and then continues. There doesn't seem to be a way to do force quit it once it runs in the ArcGIS Python console. You can't kill it using Task Manager either as the Python process doesn't show up there.

If you really want to be able to force quit it, you might want to consider using an IDE like IDLE rather than doing it in the ArcGIS Python console. In IDLE for example, you can use Ctrl + Z or Ctrl + C to terminate the execution.

1
  • 1
    The ArcGIS Pro Python window lets you stop Python code while it's executing, due to technological limitations the ArcGIS 10.X window can't. Commented Mar 6, 2015 at 3:09
9

This is the method I use:

  • create a small python script called kill_processx.py
  • When run kill_processx creates a small test file called stop_processx.txt.
  • At the top or bottom of the loop in the main program check to see if the stop_processx.txt exists.
  • If it does exist, execute any cleanup routines you need, then stop gracefully.
  • Delete the stop_processx.txt file

example of a kill_processx.py file:

import os 
nameof_killfile="stop_processx.txt"

if os.path.exists(nameof_killfile): 
    os.remove(nameof_killfile) 
else:
    killit=open(nameof_killfile,"w") 
    killit.close()
4
  • What is the script you call "kill_processx.py"?
    – Aaron
    Commented Dec 20, 2012 at 18:52
  • 1
    the kill_process script just makes an empty txt file with the correct name in the correct folder. Commented Dec 20, 2012 at 19:10
  • Could you please include a sample script in your post, so we can get a better idea of how this could help with the OP's question?
    – Aaron
    Commented Dec 20, 2012 at 19:16
  • @mhoran_psprep: I've accepted R.K.'s answer because it answers the question (it is not possible unless in an IDE). +1 for your workaround. I guess I'll implement it in future. Commented Dec 21, 2012 at 9:30
4

It is bad practice to force shutdown using brute force tactics. Rather, as @Aragon pointed out you should add error handling to your script to isolate components and/or stop the script if certain conditions are not met. As @R.K. points out, the ArcGIS python console is next to worthless for running complex scripts and, in practice, should be reserved for simple operations--stick with IDLE, pythonwin, pyscripter or any of the host of other IDEs. Here are discussions related to your question:

How do I use sys.exit(0) in an arcpy script to exit early without having an error message show up?

Terminating a Python script

How to stop a command or prompt in Python?

1
  • 1
    And of course, with a IDE like PyScripter, you can just add a "break point" at your "for...range" statement, run the script in "debug mode", and the IDE will automatically "pause" at the same point during the iterative process and allow you to inspect the results that have already been processed. Commented Dec 20, 2012 at 19:12
2

I'm working in 10.4, and I've found that I can click the close X enter image description here while a script is running. ArcMap will ask me if I want to cancel an operation.

enter image description here

I click Yes. ArcMap then asks me if I want to Save before exiting. I click Cancel. The script throws a keyboard interrupt.

2
  • Which close button do you click on in order to trigger this sequence of action? Commented Sep 25, 2017 at 22:55
  • The windows red X in the upper right of most (all?) windows software. See the small pic in my answer. Commented Sep 25, 2017 at 23:03
1

if you want to solve in pythonic way, try this:

import sys
sys.exit()

or use:

raise SystemExit()

in detail:

import time
import sys

sTime= time.time()

if time.time() - sTime > 60:
   sys.exit()

i hope it helps you...

2
  • No, this is not what I want. I want to stop the process at any time I want by pressing a keyboard combination, clicking a button or something similar while the code I have input in the console window is running. I am aware of sys.exit() but that doesn't work when a loop or a similarly long process is running. Commented Dec 20, 2012 at 12:55
  • @Upvoters: this is not the answer I'm looking for. It does not answer my question. I want to break the execution of a statement (typically iteration) by pressing a keyboard combination or clicking a button or something similar. Commented Dec 20, 2012 at 13:39
1

At least in ArcMap 10.4, you can do this. Make sure the "Results" window is visible. (If not find it under "Geoprocessing"). Now if you have a command running it will show up in this window. Right click on it and press "cancel".

0

What if under every line of code you add an if that checks if input()=="what ever you want in here" that uses sys.exit.

For example: https://repl.it/@kquazi_tcsp/JumpyNaturalMetadata

1
  • I hope this helps. Commented Jun 30, 2018 at 18:04
0

Using IDLE, I have a file called "stop.py" which contains the single line: "pass". Keep this file open in IDLE while another .py script is running. Clicking F5 in the "stop.py" window resets IDLE.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.