Python Toolbox with Many AddField() Calls Runs Slower With Attribute Window Open

579
2
Jump to solution
04-06-2021 06:00 PM
Justin_P
New Contributor III

I recently finished developing a python toolbox tool that ends up calling the arcpy.AddField_management() function about 25 times or so. I first developed it as a standalone script to get it working before I connected it up to my python toolbox. When I started testing the tool with the exact same data I found that the run time went up from ~20 seconds in standalone to ~80 seconds when run from a python toolbox inside an ArcGIS Pro map, a 4x increase. After playing around some more I found that it was definitely the AddField tool slowing it down, and then I noticed that if I close the attribute table in the Pro map the run time goes down to ~50 seconds.

So what I think is happening is that that the Pro map is busy revalidating locks and updating the GUI, etc. unnecessarily mid-script. I also notice that when the add field tools are called the "Add" and "Calculate" field buttons start alternating being grayed out, like it is cycling through locks and UI updates or something.

These alternate being grayThese alternate being gray

Is there any way to prevent this from happening? Can I somehow pause all that validation or whatever it is for the duration of my script? If not, is there a more efficient method to add fields? These short runtimes may not seem like a big deal but I now realize that I have many tools that have a LOT of calls to AddField and this is likely something that happens throughout my toolbox. I would like to make sure my code is as clean and efficient as practical.

All of the feature classes that have the add field function called on are in_memory layers, and are copied to new feature classes for the output layers.

0 Kudos
1 Solution

Accepted Solutions
DanPatterson
MVP Esteemed Contributor
2 Replies
DanPatterson
MVP Esteemed Contributor

use  Add Fields (multiple) (Data Management)—ArcGIS Pro | Documentation

and keep the table closed


... sort of retired...
Justin_P
New Contributor III

Thanks, I updated my code to use Add Fields instead of Add Field in a bunch of places and it definitely sped it up quite a bit. It is still strange that having the attribute table open slows it down a noticeable amount.

As a note to others, this function is only available in ArcGIS Pro, and the parameter order in AddFields is actually different than the original AddField, which left with with a cryptic error for quite a while before I realized that.

0 Kudos