How to force a Python tool to brake loop and do cleanup when user clicks "Close"?
I am writing a Python tool that perform heavy tasks in a loop. When the user clicks "Close" I want to break the loop, perform some cleanup, and then finish.
Consider this example tool that counts down from ten:
import arcpyimport timeclass Tool(object): def __init__(self): self.label = "Cancel Test" self.description = "Tests how the cancel function works." self.canRunInBackground = False def execute(self, parameters, messages): for i in range(10, -1, -1): arcpy.AddMessage(i) time.sleep(1) arcpy.AddMessage("We have lift-off!") returnAccording to the docuemntation:
Completed script CancelTest...Cancelled function(CancelTest) aborted by User.Failed at [TIME] (Elapsed Time: 10,01 seconds)So I tried, as suggested in the documentation linked above, to use isCancelled instead:
import arcpyimport time#Make sure it does not auto cancel.arcpy.env.autoCancelling = Falseclass Tool(object): def __init__(self): self.label = "Cancel Test" self.description = "Tests how the cancel function works." self.canRunInBackground = False def execute(self, parameters, messages): for i in range(10, -1, -1): arcpy.AddMessage(i) time.sleep(1) #Check if the user clicked "Close" if arcpy.env.isCancelled: arcpy.AddMessage("Launch aborted!") return arcpy.AddMessage("We have lift-off!") returnHowever, this gives me the following error:
Traceback (most recent call last): File "H:\Mina Dokument\DGD\Python\CancelTest.py", line 19, in execute if arcpy.env.isCancelled:AttributeError: 'GPEnvironment' object has no attribute 'isCancelled'It seems that neither of my two versions works as expected. Why? And how can I make this work as I want it to?
أكثر...
I am writing a Python tool that perform heavy tasks in a loop. When the user clicks "Close" I want to break the loop, perform some cleanup, and then finish.
Consider this example tool that counts down from ten:
import arcpyimport timeclass Tool(object): def __init__(self): self.label = "Cancel Test" self.description = "Tests how the cancel function works." self.canRunInBackground = False def execute(self, parameters, messages): for i in range(10, -1, -1): arcpy.AddMessage(i) time.sleep(1) arcpy.AddMessage("We have lift-off!") returnAccording to the docuemntation:
By default, when a script tool or Python toolbox tool is cancelled, the script will be cancelled after the current line of code and the tool will fail with an appropriate error message
But when I click "Close" in the beginning of the execution, it still loops to the very end and adds the message "We have lift-off!" before it outputs this:
Completed script CancelTest...Cancelled function(CancelTest) aborted by User.Failed at [TIME] (Elapsed Time: 10,01 seconds)So I tried, as suggested in the documentation linked above, to use isCancelled instead:
import arcpyimport time#Make sure it does not auto cancel.arcpy.env.autoCancelling = Falseclass Tool(object): def __init__(self): self.label = "Cancel Test" self.description = "Tests how the cancel function works." self.canRunInBackground = False def execute(self, parameters, messages): for i in range(10, -1, -1): arcpy.AddMessage(i) time.sleep(1) #Check if the user clicked "Close" if arcpy.env.isCancelled: arcpy.AddMessage("Launch aborted!") return arcpy.AddMessage("We have lift-off!") returnHowever, this gives me the following error:
Traceback (most recent call last): File "H:\Mina Dokument\DGD\Python\CancelTest.py", line 19, in execute if arcpy.env.isCancelled:AttributeError: 'GPEnvironment' object has no attribute 'isCancelled'It seems that neither of my two versions works as expected. Why? And how can I make this work as I want it to?
أكثر...