Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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.

share|improve this question
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
add comment

4 Answers

up vote 3 down vote accepted

UPDATE 2:

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.


UPDATE:

According to the text at the right side of the console, ESC should cancel the current operation.


If by stop you mean force it to terminate then I think Ctrl + Z should do the trick. Otherwise, you'll need to find the Python process and kill it.

share|improve this answer
    
In this situation, you gave a wrong answer to the question so I downvoted it. I would appreciate it if you do not accuse me of downvoting you for all those other times. I only downvote people's answers when I think they are wrong or misleading. I do not downvote to punish people. That's just wrong. –  R.K. Dec 20 '12 at 13:44
    
+1 Python IDE is the way to go –  Aaron Dec 20 '12 at 20:38
add comment

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()
share|improve this answer
    
What is the script you call "kill_processx.py"? –  Aaron Dec 20 '12 at 18:52
1  
the kill_process script just makes an empty txt file with the correct name in the correct folder. –  mhoran_psprep Dec 20 '12 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 Dec 20 '12 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. –  Alex Essilfie Dec 21 '12 at 9:30
add comment

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...

share|improve this answer
    
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
add comment

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?

share|improve this answer
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. –  RyanDalton Dec 20 '12 at 19:12
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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