pythonidearcpy

Arcpy script was fast now slow?


Question pretty much sums it up. I had the following script running in PyCharm earlier today:

rasterfolder = "F:/1_Raster_Processing/Classified/"
vectorfolder = "H:/Output/SplitShapes/"
tablefolder = "H:/Output/Split_Tables_3/"


import arcview
import arcpy
from arcpy.sa import *
arcpy.CheckOutExtension("spatial")


arcpy.env.workspace = vectorfolder
flist = arcpy.ListFeatureClasses()
for f in flist:
    TabulateArea(f,"FID",rasterfolder + "KNN100_" + f[:-6] + ".tif","Value",tablefolder + f[:-4] + ".dbf",0.25)

It was taking ~4 minutes a featureclass and there are a couple hundred to run through. At some point in the middle of the day, the process hung and wasn't generating any more output, so I killed it and started over (thinking this was a pyCharm issue). When I started over, it was suddenly taking ~1hr+ per feature class to do the exact same process. I've tried the same process in WingIDE and I'm still having the issue. I've restarted everything and am running nothing but this one process on this machine. Thoughts? Do I need to sacrifice a barnyard animal here? Pay for winRar?

Has any one else had an issue where they run a script; it runs fine the first few times, but then suddenly, and for no apparent reason, it takes a massive slowdown?


Solution

  • Ok. Not sure this deserves the down-votes its getting since it's a valid question, but to all the -1's out there: This is a legitimate issue I was able to consistently reproduce in arcpy across several different machines. So if you've been trying to figure out wtf is going on with highly variable processing times for an arcpy script, read on.

    Turns out, there was a legitimate memory leak issue happening with the TabulateArea function. Try adding something like this in your for loop:

    arcpy.env.workspace = 'in_memory' #Outside the loop
    
    arcpy.Delete_management("in_memory") #after the function completes
    

    Hope this helps somebody. Drove me bonkers for days trying to figure this out.