Select to view content in your preferred language

Python Toolbox - ArcGIS Pro layer locked after subsequent runs - what is stored in "memory"?

1698
8
04-25-2022 11:43 AM
I_AM_ERROR
Regular Contributor

I have a python toolbox that is performing various operations on a layout. Reconfigure text elements, pan the map frame, export to PDF being the core functionality. Editing data is not involved.

The tool runs fine initially, but after trying a second run one of the map layers is "locked" in the layout which is causing various issues with tool execution.

My question is: Is Pro keeping variables, objects, classes, etc. in memory between tool runs? If so, how do I clear this so each tool run is new? Is the python process left running once the toolbox is opened?

Additional details:

  • closing Pro and running the tool again remedies the issue - lock is only applied after second run
  • Used del() to remove object and variables followed by gc.collect() at the end of the toolbox code execute to no avail
  • my IDE is VS Code - running here using the approach mentioned here does not present the issue, assuming this is due to each run being a distinct process run and closure, with nothing mainted in between
Tags (2)
8 Replies
DanPatterson
MVP Esteemed Contributor

If it is created and added to in memory, then you can use

Delete (Data Management)—ArcGIS Pro | Documentation

del and gc won't occur until the script/tool is done.

Also, if something is being created, you can use the "overwriteOutput" environment variable


... sort of retired...
0 Kudos
by Anonymous User
Not applicable

According to the mapping docs: 

When you use the CURRENT keyword in the Python window with ArcGIS Desktop, you sometimes had to call refreshActiveView to force a screen refresh. This is no longer the case with ArcGIS Pro. All arcpy.mp API changes directly update ArcGIS Pro.

 I haven't tried this yet in Pro, but maybe something like 

 

aprx = arcpy.mp.ArcGISProject("CURRENT")
aprx.save()

 

at the end of your code to force the changes?

0 Kudos
DonMorrison1
Frequent Contributor

I'm quite sure a new process is not started to run a toolbox tool, so yes, python is keeping things in memory - specially those created at the toolbox/tool class level and even in the getParameterInfo method. I've found number of places where locks are left dangling (even reported some with no resolution) and can't find a way around them other than to restart ArcGIS Pro (or restart the python kernel if running in the IDE) - good luck with this one!

I_AM_ERROR
Regular Contributor

I think I traced it down something being done in the update parameters method which I can't debug in IDE, either:

  • arcpy.SelectLayerByAttribute_management
  • arcpy.ListFields
  • or arcpy.da.SearchCursor (used via list comprehension)

my guess is the cursor is being left open when used via list comprehension. 

obligatory - "code was inherited from someone else!"

0 Kudos
DanPatterson
MVP Esteemed Contributor

SearchCursor—ArcGIS Pro | Documentation

use the "with" syntax for cursors...

Search cursors also support with statements to reset iteration and aid in removal of locks. 


... sort of retired...
by Anonymous User
Not applicable

my guess is the cursor is being left open when used via list comprehension. 

That could be an important detail - cursors are usually called using `with` to ensure the entry and exit get hit. Perhaps it's dangling if not closed properly? Curious to see if that caused it.

I_AM_ERROR
Regular Contributor

It was - well, it's currently commented out and execution is running and not leaving layer locks in the map anymore. If I re-enable I'll use "with" and report back.

0 Kudos
SarahGe
Occasional Contributor

I have a similar issue. I have a python toolbox tool that downloads multidimensional rasters via API calls. Then it merges those rasters using the merge multidimensional rasters tool. If I then try to run the tool again, intending to overwrite everything,  the raster download fails because it can't overwrite the existing rasters which are "being  used by another process". The other process is the merge operation (re-running the tool worked fine before I added the merge) which is long finished at that point.  The only thing that seems to release the lock is closing and opening ArcGIS Pro. I don't know how I could use a "with" statement for merging a raster.
I tried adding arcpy.env.overwriteOutput = True to my script but that didn't help.
Did you ever find a way of getting rid of the lock?

0 Kudos