Close event passed to python script when canceling toolbox script

521
1
04-11-2012 05:29 PM
MichaelO_Donnell1
New Contributor II
I have written a program that spawns jobs and I would like to kill these jobs when the native python script (invoked from toolbox) is canceled from the results tab of ArcMap or ArcCatalog. First, I am unclear as to what type of signal is passed to the script that enables ESRI to terminate it, and second I am not sure how to detect the event and then run a clean up based on this signal. Has anyone tried this before. There was a post on ESRI a couple years ago but no one figured it out and therefore there was no posted resolution.

I believe I might be able to use win32api, but I have not figured it out yet. For example:
win32api.SetConsoleCtrlHandler(func, True)
Tags (2)
0 Kudos
1 Reply
curtvprice
MVP Esteemed Contributor
. First, I am unclear as to what type of signal is passed to the script that enables ESRI to terminate it, and second I am not sure how to detect the event and then run a clean up based on this signal.


Back then in 2007 (I think this is the post) was a while ago. I think the glue is much better now.

I'm pretty sure what's passed now would be arcpy.ExecuteError like any other tool failure. You can trap for that exception class, but you would then want to search the error message strings for error codes from the arcpy.getMessages() array to verify the cancel was what generated the ExecuteError. I'd just try it with your script to find out which particular error code pops up from your cancel, in case it depends on context in your script. For example, if you get error 080522 you would scan through the messages for the text " 080522:".

Update: I tried it this morning and it's true, if a tool isn't running when Cancel is pressed, the script process just dies, with no error message or anything saved in the results. That's not good as it looks like ArcGIS is just zapping the subprocess with a kill. I have no advice, sorry, and hope someone (from Esri) can chime in that knows some of the internals here.

I believe I might be able to use win32api, but I have not figured it out yet. For example:
win32api.SetConsoleCtrlHandler(func, True)


I believe this was non-trivial on Windows platforms in the past, but Python 2.6 includes a nifty Popen.kill() method on the subprocess module.
0 Kudos