I am trying to build more fail-management logic into some of the GP services I develop. As a part of the business logic within the script tool I build, I write some data into the tables and thus lock access to certain parts of the geodatabase to let the currently running GP task to be executed undisturbed.

However, in case there are some issues which will force the GP service instance or ArcGIS Server Windows service to crash (something that is out of my hands), I want to be able to perform some clean-up work (release locks, clean-up scratch geodatabases, write to tables, etc). This is where I usually use finally: statement when developing Python script tools for use in ArcGIS Desktop.

However, the GP service does not seem to reach the finally: if it is timed-out or crashed. The Python script tool source code is below:

import arcpy
import time
    arcpy.AddMessage("Hello 1 second")
    arcpy.AddMessage("Hello after 10 sec")
    arcpy.AddMessage("GP service timed out!")

I have run this tool in ArcMap:

enter image description here

Trying to run the GP service task (published from the tool result) that of GP service with time-out time for 9 seconds:

enter image description here

So the GP service is not able to reach finally statement and print "Hello after 10 sec". If this is not possible to implement within the finally: statement in Python script tool source code, how would you approach this otherwise?

1 Answer 1


What you're asking for isn't possible. ArcGIS server supervises the Python process and kills it if it takes too long. It doesn't send an interrupt/break to the Python session so the Python interpreter would never get to that finally block. I know you said it's out of your control, but you may want to beg the administrators of your server to let you tick up the timeout of the service to a larger value.

  • thanks. I've kind of suspected that this might be the case. How do I handle a GP service instance that crashes (software crash) and leave lots of data in the incorrupted state? I do use SQL for some parts of the work, so I am safe with transactions there, but other things are just being processed in binary and can easily be left in an invalid state. Anything that can help me at least come closer to what I've wanted to achieve with finally? It can be done outside of the Python script tool, of course. Commented Oct 20, 2014 at 15:24
  • I don't know the best practices for that. You might want to ask another question here or on GeoNet to see if someone who knows can help. Commented Oct 20, 2014 at 15:51

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.