Working on a script to generate a series of property record card PDFs from a map series using multiprocessing. Learned about multiprocessing in an Advanced Python class and thought it could be used to help with this project. Has to be run nightly on approx. 3,300 parcels, but is taking 12+ hours due to slowing drastically as it runs. Memory also increases, but have not been able to locate memory leak (newer to Python) and Spyder locking up. Written using PyScripter and Python 3.6.12 . Ideally, would be able to be run once a year to generate two PDFs for each of 75,000 parcels.
Developing on a laptop with Intel(R) Core(TM) i7-6820HQ CPU @ 2.70GHz 2.70 GHz processor and 32 GB RAM / 8 cpu.
Below is the function. It also has to look for building sketches and attach them to the resulting card if they exist.
The Parcel identifier is fed into the map series, generates 3 pages, and then looks for building sketches.
Looking for any guidance, function and main script attached as zip file.
Here is the function:
import os, sys
import arcpy
arcpy.env.overwriteOutput = True
def worker(mxdPath, outFolder, name):
"""
This is the function that gets called and does the work of exporting the DDP for the UPI specified.
Note that this function does not try to write to arcpy.AddMessage() as nothing is ever displayed. If the cexport succeeds then it returns TRUE else FALSE.
"""
try:
mxdFile = arcpy.mp.ArcGISProject(mxdPath)
cleanName = name.replace("/", "_")
finalPDF = os.path.join(outFolder, cleanName + ".pdf")
l = mxdFile.listLayouts("Page_1")[0]
if not l.mapSeries is None:
ms = l.mapSeries
if ms.enabled:
ms.currentPageNumber = ms.getPageNumberFromName(name)
file1 = os.path.join(outFolder, cleanName)
ms.exportToPDF(file1, "CURRENT", resolution=300)
ms.refresh()
pdfDoc = arcpy.mp.PDFDocumentOpen(finalPDF)
l = mxdFile.listLayouts("Page_2")[0]
if not l.mapSeries is None:
ms = l.mapSeries
if ms.enabled:
ms.currentPageNumber = ms.getPageNumberFromName(name)
pageTwo = cleanName + "t2"
file2 = os.path.join(outFolder, pageTwo + ".pdf")
ms.exportToPDF(file2, "CURRENT", resolution=300)
pdfDoc.appendPages(file2)
os.remove(file2)
#ms.refresh()
try:
resSketch = ms.pageRow.SKETCH
if os.path.exists(resSketch):
l = mxdFile.listLayouts("Page_3")[0]
if not l.mapSeries is None:
ms = l.mapSeries
if ms.enabled:
ms.currentPageNumber = ms.getPageNumberFromName(name)
pageThree = cleanName + "t3"
file3 = os.path.join(outFolder, pageThree + ".pdf")
ms.exportToPDF(file3, "CURRENT", resolution=300)
pdfDoc.appendPages(file3)
os.remove(file3)
del file3, resSketch
except:
pass
try:
#Checks for existence of Sketch PDF & appends it to new PDF
commSketch = ms.pageRow.COMMERCIAL_SKETCH
pdfDoc.appendPages(commSketch)
except:
pass
pdfDoc.saveAndClose()
del pdfDoc, file1, file2, mxdPath, resSketch
del mxdFile
return True
except arcpy.ExecuteError:
# Geoprocessor threw an error
arcpy.AddError(arcpy.GetMessages(2))
print("Execute Error:", arcpy.ExecuteError)
except Exception as e:
tb = sys.exc_info()[2]
print("Failed at Line %i \n" % tb.tb_lineno)
print("Error: {} \n".format(e))
return False