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.

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
Alex Essilfie
  • 369
  • 2
  • 5
  • 13
  • 1
    By break do you mean do you mean force it to quit? – R.K. Dec 20 '12 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. – Alex Essilfie Dec 20 '12 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. Dec 20 '12 at 13:02
  • use ctrl +Z in lx terminal for windows machine –  Oct 04 '15 at 07: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. – Alex Essilfie Oct 16 '15 at 11:59

8 Answers8

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.

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
R.K.
  • 17,405
  • 3
  • 59
  • 110
  • 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. – Jason Scheirer Mar 06 '15 at 03: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()
mhoran_psprep
  • 1,001
  • 1
  • 6
  • 9
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?

Aaron
  • 51,658
  • 28
  • 154
  • 317
  • 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. – RyanKDalton Dec 20 '12 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.

Emil Brundage
  • 13,859
  • 3
  • 26
  • 62
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".

H.Durham
  • 111
  • 2
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...

urcm
  • 22,533
  • 4
  • 57
  • 109
  • 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. – Alex Essilfie Dec 20 '12 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. – Alex Essilfie Dec 20 '12 at 13:39
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

Marcelo Villa
  • 5,928
  • 2
  • 19
  • 38
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.